Jump to content
McKay Development

Cannot read property 'createOffer' of undefined


xawa

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...