Schmaniel
Schmaniel

Reputation: 105

Why am I getting a TypeError with promisify

I'm using promisify and get the TypeError: this._send is not a function

The Error only happens with const ranking = await requestPlayersProfile(account_id);

Does anyone have any ideas why it is not working?

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));
        });
    });
};

Upvotes: 0

Views: 96

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

Since the callback to csgo.requestPlayersProfile doesn't follow the "node" pattern function(err, result) - you'll want to promisify that yourself like this:

const requestPlayersProfile = (csgo, steamid) => new Promise(resolve => csgo.requestPlayersProfile(steamid, resolve));

usage

const ranking = await requestPlayersProfile(csgo, steamid);

Upvotes: 1

Related Questions