为什么我在使用 promisify 时会出现 TypeError
Why am I getting a TypeError with promisify
我正在使用 promisify 并获得 TypeError: this._send is not a function
错误只发生在 const ranking = await requestPlayersProfile(account_id);
有没有人知道为什么它不起作用?
const SteamUser = require('steam-user');
const GlobalOffensive = require('globaloffensive');
let user = new SteamUser();
let csgo = new GlobalOffensive(user);
var pool = mysql.createPool({
supportBigNumbers : true,
bigNumberStrings : true,
connectionLimit : 100,
connectTimeout : 20000,
acquireTimeout : 20000,
host : db.host,
user : db.user,
password : db.password,
database : db.dbname
});
const { promisify } = require('util');
const requestPlayersProfile= promisify(csgo.requestPlayersProfile);
const getConnection = promisify(pool.getConnection);
const query = (connection, sql, args) => new Promise((resolve, reject) => {
connection.query(sql, args, err => {
connection.release();
if (err) {
reject(err);
} else {
resolve();
}
});
});
if ( csgo.haveGCSession ) {
pool.getConnection(function(err, connection) {
if (err) throw err;
connection.query("SELECT * FROM Users", function (err, rows, fields) {
connection.release();
if (err) throw err;
async function doit() {
for (let row of rows) {
var account_id = new SteamID(`${row.SteamID64}`);
csgo.setMaxListeners(50);
const ranking = await requestPlayersProfile(account_id);
var rankid = ranking.ranking.rank_id;
//Convert MM rankid
if (rankid == 0) {
var rank = "Unranked";
} else if (rankid == 1) {
var rank = "Silver 1";
} else if (rankid == 2) {
var rank = "Silver 2";
} else if (rankid == 3) {
var rank = "Silver 3";
} else if (..) {
..
} else if (rankid == 18) {
var rank = "Global Elite";
}
const connection = await getConnection();
await query(connection, "UPDATE Users SET CSGOMM=? WHERE SteamID64=?", [rank, `${row.SteamID64}`]);
}
}
doit().catch(err => console.error(err));
});
});
};
由于 csgo.requestPlayersProfile
的回调不遵循
“节点”模式 function(err, result)
- 你会想这样承诺自己:
const requestPlayersProfile = (csgo, steamid) => new Promise(resolve => csgo.requestPlayersProfile(steamid, resolve));
用法
const ranking = await requestPlayersProfile(csgo, steamid);
我正在使用 promisify 并获得 TypeError: this._send is not a function
错误只发生在 const ranking = await requestPlayersProfile(account_id);
有没有人知道为什么它不起作用?
const SteamUser = require('steam-user');
const GlobalOffensive = require('globaloffensive');
let user = new SteamUser();
let csgo = new GlobalOffensive(user);
var pool = mysql.createPool({
supportBigNumbers : true,
bigNumberStrings : true,
connectionLimit : 100,
connectTimeout : 20000,
acquireTimeout : 20000,
host : db.host,
user : db.user,
password : db.password,
database : db.dbname
});
const { promisify } = require('util');
const requestPlayersProfile= promisify(csgo.requestPlayersProfile);
const getConnection = promisify(pool.getConnection);
const query = (connection, sql, args) => new Promise((resolve, reject) => {
connection.query(sql, args, err => {
connection.release();
if (err) {
reject(err);
} else {
resolve();
}
});
});
if ( csgo.haveGCSession ) {
pool.getConnection(function(err, connection) {
if (err) throw err;
connection.query("SELECT * FROM Users", function (err, rows, fields) {
connection.release();
if (err) throw err;
async function doit() {
for (let row of rows) {
var account_id = new SteamID(`${row.SteamID64}`);
csgo.setMaxListeners(50);
const ranking = await requestPlayersProfile(account_id);
var rankid = ranking.ranking.rank_id;
//Convert MM rankid
if (rankid == 0) {
var rank = "Unranked";
} else if (rankid == 1) {
var rank = "Silver 1";
} else if (rankid == 2) {
var rank = "Silver 2";
} else if (rankid == 3) {
var rank = "Silver 3";
} else if (..) {
..
} else if (rankid == 18) {
var rank = "Global Elite";
}
const connection = await getConnection();
await query(connection, "UPDATE Users SET CSGOMM=? WHERE SteamID64=?", [rank, `${row.SteamID64}`]);
}
}
doit().catch(err => console.error(err));
});
});
};
由于 csgo.requestPlayersProfile
的回调不遵循
“节点”模式 function(err, result)
- 你会想这样承诺自己:
const requestPlayersProfile = (csgo, steamid) => new Promise(resolve => csgo.requestPlayersProfile(steamid, resolve));
用法
const ranking = await requestPlayersProfile(csgo, steamid);