Jump to content


Photo
Question

Cannot read property 'createOffer' of undefined

node.js node-steam-tradeoffer-manager

  • Please log in to reply
2 replies to this topic

#1 xawa

xawa

    Newbie

  • Member
  • Pip
  • 8 posts

Posted 10 April 2019 - 12:51 PM

Hello there. I'm trying to do my own script but getting error always. Also "this.tradeUrl" consoling undefined.

const SteamUser = require('steam-user');
const SteamCommunity = require('steamcommunity');
const SteamTotp = require('steam-totp');
const TradeManager = require('steam-tradeoffer-manager');

var self = null;

var BotHandler = function(botConfig, tradeUrl, ownItems, theirItems) {
	this.tradeUrl = tradeUrl;
        console.log(this.tradeUrl) // this gives correct trade URL
	this.ownItems = ownItems;
	this.theirItems = theirItems;
	this.config = botConfig;
	this.client = new SteamUser();
	this.manager = new TradeManager({
		'steam': this.client, 
		'domain': 'xx',
		'language': 'tr'
	});
	this.community = new SteamCommunity();
	
	this.logOnOptions = {
		'accountName': botConfig.username,
		'password': botConfig.password,
		'twoFactorCode': SteamTotp.getAuthCode(botConfig.secret)
	};

	self = this;
};

BotHandler.prototype.start = function() {
	this.client.on('webSession', this.trade);
	this.client.logOn(this.logOnOptions);
};

BotHandler.prototype.trade = function(sessionId, cookies) {
	self.manager.setCookies(cookies, self.getInventory);
};

BotHandler.prototype.getInventory = function(error) {
	if(error) {
		throw error;
		return;
	}

	self.manager.getInventoryContents(self.config.appId, 
		self.config.contextId, true, self.createOffer);
};

BotHandler.prototype.createOffer = function(error, inventory) {
	console.log('gg');
	console.log(this.tradeUrl); // this gives "UNDEFINED"
	if(error) {
		throw error;
		return;
	}
	var offer = this.manager.createOffer(this.tradeUrl);

	for(var i = 0; i < self.ownItems.length; i++) {
		offer.addMyItem({
			assetid: self.ownItems[i],
			appid: self.config.appId,
			contextid: self.config.contextId,
			amount: 1
		});
	}

	for(var i = 0; i < self.theirItems.length; i++) {
		offer.addTheirItem({
			assetid: self.theirItems[i],
			appid: self.config.appId,
			contextid: self.config.contextId,
			amount: 1
		});
	}

	offer.send();
};

BotHandler.prototype.acceptTrade = function(error, status) {
	if(err) {
		throw error;
		return;
	}

	if(status == 'pending') {
		console.log("pending");
	}else {
		console.log('sent');
	}
};

module.exports = BotHandler;

What am i missing?

 

Output Error 

 var offer = this.manager.createOffer(tradeUrl);
                                 ^

TypeError: Cannot read property 'createOffer' of undefined
    at BotHandler.createOffer (F:\Xamp\htdocs\steam-trade-bot\BotHandler.js:56:27)
    at SteamCommunity.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\users.js:484:5)
    at Request._callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\http.js:67:15)
    at Request.self.callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:185:22)
    at Request.emit (events.js:189:13)
    at Request.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1161:10)
    at Request.emit (events.js:189:13)
    at Gunzip.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:277:13)
    at Gunzip.emit (events.js:194:15)

F:\Xamp\htdocs\steam-trade-bot>node bot.js --botId 0 --tradeUrl "https://steamcommunity.com/tradeoffer/new/?partner=100331693&token=a6vNScw-" --ownList 5854236464 --theirList 15702811262
https://steamcommunity.com/tradeoffer/new/?partner=100331693&token=a6vNScw-
undefined
F:\Xamp\htdocs\steam-trade-bot\BotHandler.js:56
        var offer = this.manager.createOffer(tradeUrl);
                                 ^

TypeError: Cannot read property 'createOffer' of undefined
    at BotHandler.createOffer (F:\Xamp\htdocs\steam-trade-bot\BotHandler.js:56:27)
    at SteamCommunity.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\users.js:484:5)
    at Request._callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\http.js:67:15)
    at Request.self.callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:185:22)
    at Request.emit (events.js:189:13)
    at Request.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1161:10)
    at Request.emit (events.js:189:13)
    at Gunzip.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:277:13)
    at Gunzip.emit (events.js:194:15)

F:\Xamp\htdocs\steam-trade-bot>node bot.js --botId 0 --tradeUrl "https://steamcommunity.com/tradeoffer/new/?partner=100331693&token=a6vNScw-" --ownList 5854236464 --theirList 15702811262
gg
undefined
F:\Xamp\htdocs\steam-trade-bot\BotHandler.js:56
        var offer = this.manager.createOffer(this.tradeUrl);
                                 ^

TypeError: Cannot read property 'createOffer' of undefined
    at BotHandler.createOffer (F:\Xamp\htdocs\steam-trade-bot\BotHandler.js:56:27)
    at SteamCommunity.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\users.js:484:5)
    at Request._callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\http.js:67:15)
    at Request.self.callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:185:22)
    at Request.emit (events.js:189:13)
    at Request.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1161:10)
    at Request.emit (events.js:189:13)
    at Gunzip.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:277:13)
    at Gunzip.emit (events.js:194:15)

F:\Xamp\htdocs\steam-trade-bot>node bot.js --botId 0 --tradeUrl "https://steamcommunity.com/tradeoffer/new/?partner=100331693&token=a6vNScw-" --ownList 5854236464 --theirList 15702811262
gg
undefined
F:\Xamp\htdocs\steam-trade-bot\BotHandler.js:56
        var offer = this.manager.createOffer(this.tradeUrl);
                                 ^

TypeError: Cannot read property 'createOffer' of undefined
    at BotHandler.createOffer (F:\Xamp\htdocs\steam-trade-bot\BotHandler.js:56:27)
    at SteamCommunity.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\users.js:484:5)
    at Request._callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\http.js:67:15)
    at Request.self.callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:185:22)
    at Request.emit (events.js:189:13)
    at Request.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1161:10)
    at Request.emit (events.js:189:13)
    at Gunzip.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:277:13)
    at Gunzip.emit (events.js:194:15)

F:\Xamp\htdocs\steam-trade-bot>
F:\Xamp\htdocs\steam-trade-bot>
F:\Xamp\htdocs\steam-trade-bot>node bot.js --botId 0 --tradeUrl https://steamcommunity.com/tradeoffer/new/?partner=100331693&token=a6vNScw- --ownList 5854236464 --theirList 15702811262
gg
undefined
F:\Xamp\htdocs\steam-trade-bot\BotHandler.js:56
        var offer = this.manager.createOffer(this.tradeUrl);
                                 ^

TypeError: Cannot read property 'createOffer' of undefined
    at BotHandler.createOffer (F:\Xamp\htdocs\steam-trade-bot\BotHandler.js:56:27)
    at SteamCommunity.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\users.js:484:5)
    at Request._callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\http.js:67:15)
    at Request.self.callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:185:22)
    at Request.emit (events.js:189:13)
    at Request.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1161:10)
    at Request.emit (events.js:189:13)
    at Gunzip.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:277:13)
    at Gunzip.emit (events.js:194:15)
'token' is not recognized as an internal or external command,
operable program or batch file.

F:\Xamp\htdocs\steam-trade-bot>node bot.js --botId 0 --tradeUrl 'https://steamcommunity.com/tradeoffer/new/?partner=100331693&token=a6vNScw-' --ownList 5854236464 --theirList 15702811262
gg
undefined
F:\Xamp\htdocs\steam-trade-bot\BotHandler.js:56
        var offer = this.manager.createOffer(this.tradeUrl);
                                 ^

TypeError: Cannot read property 'createOffer' of undefined
    at BotHandler.createOffer (F:\Xamp\htdocs\steam-trade-bot\BotHandler.js:56:27)
    at SteamCommunity.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\users.js:484:5)
    at Request._callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\steamcommunity\components\http.js:67:15)
    at Request.self.callback (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:185:22)
    at Request.emit (events.js:189:13)
    at Request.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1161:10)
    at Request.emit (events.js:189:13)
    at Gunzip.<anonymous> (F:\Xamp\htdocs\steam-trade-bot\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:277:13)
    at Gunzip.emit (events.js:194:15)
'token' is not recognized as an internal or external command,
operable program or batch file.



#2 Dr. McKay

Dr. McKay

    Developer

  • Administrator
  • 2,042 posts

Posted 10 April 2019 - 04:49 PM

You're not using this properly. In JavaScript, this references the parent object of the method at call-time. So for example:

function exampleMethod() {
	console.log(this.name);
}
	
let obj1 = {"name": "obj1"};
let obj2 = {"name": "obj2"};

obj1.example = exampleMethod;
obj2.example = exampleMethod;

obj1.example(); // "obj1" because the "parent" to this call is obj1
obj2.example(); // "obj2" because the "parent" to this call is obj2
let example = obj1.example;
example(); // undefined because there is no "parent" to example() in this call

So when you do getInventoryContents(foo, bar, this.createOffer), when the module receives the callback, it sees this:

TradeOfferManager.prototype.getInventoryContents(blah, callback) {
    // do stuff
    callback(); // no parent! so "this" will not be what you expect
};

What you should do instead is: getInventoryContents(foo, bar, this.createOffer.bind(this)). Using the bind method "binds" the value of this before call-time.


  • xawa likes this

#3 xawa

xawa

    Newbie

  • Member
  • Pip
  • 8 posts

Posted 11 April 2019 - 05:25 AM

You're not using this properly. In JavaScript, this references the parent object of the method at call-time. So for example:

function exampleMethod() {
	console.log(this.name);
}
	
let obj1 = {"name": "obj1"};
let obj2 = {"name": "obj2"};

obj1.example = exampleMethod;
obj2.example = exampleMethod;

obj1.example(); // "obj1" because the "parent" to this call is obj1
obj2.example(); // "obj2" because the "parent" to this call is obj2
let example = obj1.example;
example(); // undefined because there is no "parent" to example() in this call

So when you do getInventoryContents(foo, bar, this.createOffer), when the module receives the callback, it sees this:

TradeOfferManager.prototype.getInventoryContents(blah, callback) {
    // do stuff
    callback(); // no parent! so "this" will not be what you expect
};

What you should do instead is: getInventoryContents(foo, bar, this.createOffer.bind(this)). Using the bind method "binds" the value of this before call-time.

 

Thx man i've solved.







Also tagged with one or more of these keywords: Question, node.js, node-steam-tradeoffer-manager

2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users