liamcottle / rustplus.js

Unofficial NodeJS library for controlling Smart Switches in the PC game Rust
228 stars 46 forks source link

Cannot read property 'fromObject' of undefined when running requests #1

Closed PhilippHeuer closed 4 years ago

PhilippHeuer commented 4 years ago

Hey, i wanted to play around with this a little and made a new project in which i created a index.js and run the require command from the readme - it looks like this:

const RustPlus = require('rustplus-api');
var rustplus = new RustPlus('ip', port, '<steamid>', '<token>');

rustplus.on('message', (message) => {
    console.log("message received: " + JSON.stringify(message));
});

rustplus.getEntityInfo(1234567, (message) => {
    console.log("getEntityInfo response message: " + JSON.stringify(message));
    return true;
});

rustplus.sendTeamMessage('Hello World!', (message) => {
    console.log(message);
});

But whenever i call a function that would trigger a request i'm getting this error before it's even executed:

node index.js
H:\SourceCodes\Games\Rust\node-rustplus-test\node_modules\rustplus-api\rustplus.js:130
        let message = this.AppRequest.fromObject(payload);
                                      ^

TypeError: Cannot read property 'fromObject' of undefined
    at RustPlus.sendRequest (H:\SourceCodes\Games\Rust\node-rustplus-test\node_modules\rustplus-api\rustplus.js:130:39)
    at RustPlus.getEntityInfo (H:\SourceCodes\Games\Rust\node-rustplus-test\node_modules\rustplus-api\rustplus.js:193:14)
    at Object.<anonymous> (H:\SourceCodes\Games\Rust\node-rustplus-test\index.js:21:10)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

Do you know what i'm doing wrong?

liamcottle commented 4 years ago

Oops, I'll have to update the docs on this. You need to wait until everything is setup before you can send requests. Just move your calls so they happen after the connected event is fired.

const RustPlus = require('rustplus-api');
var rustplus = new RustPlus('ip', port, '<steamid>', '<token>');

rustplus.on('message', (message) => {
    console.log("message received: " + JSON.stringify(message));
});

// when connected to rust server
rustplus.on('connected', () => {

    rustplus.getEntityInfo(1234567, (message) => {
        console.log("getEntityInfo response message: " + JSON.stringify(message));
        return true;
    });

    rustplus.sendTeamMessage('Hello World!', (message) => {
        console.log(message);
    });

});
PhilippHeuer commented 4 years ago

Thanks a lot for the quick response, it works like this :)