MoralisWeb3 / Moralis-JS-SDK

Moralis Official Javascript SDK
https://docs.moralis.io
Other
368 stars 257 forks source link

TypeError: Cannot read property 'length' of undefined #1

Closed teneko closed 3 years ago

teneko commented 3 years ago

Assuming this

get moralis(): Moralis {
    if (this._moralis != null) {
        return this._moralis;
    }
    this._moralis = moralis;
    this._moralis.initialize(processEnvironmentVariables.MORALIS_APPLICATION_ID, null, processEnvironmentVariables.MORALIS_MASTER_KEY); // javaScriptKey is null because moralis server
    this._moralis.serverURL = processEnvironmentVariables.MORALIS_SERVER_URL;
    logger.debug("Moralis has been set and initialized");
    return this._moralis;
}

and this

const page_size = 500;
let page = 0;

var result = await this.context.moralis.Web3API.token.getNFTOwners({
    chain: "bsc",
    address: this.market.nftAddress,
    format: "hex",
    order: "owner_of.ASC",
    limit: page_size,
    offset: page * page_size
});

I get an exception:

TypeError: Cannot read property 'length' of undefined
    at Object.request (C:\Users\<repo>\node_modules\moralis\lib\node\RESTController.js:311:17)
    at Object.run (C:\Users\<repo>\node_modules\moralis\lib\node\Cloud.js:167:36)
    at run (C:\Users\<repo>\node_modules\moralis\lib\node\Cloud.js:93:52)
    at Object.wrappers.<computed>.<computed> [as getNFTOwners] (C:\Users\<repo>\node_modules\moralis\lib\node\MoralisWeb3Api.js:169:29)
    at <user-code>

What am I doing wrong?

teneko commented 3 years ago

Without a custom server created from your website, it won't work either:

get moralis(): Moralis {
    if (this._moralis != null) {
        return this._moralis;
    }
    this._moralis = moralis;
    this._moralis.initialize(processEnvironmentVariables.MORALIS_APPLICATION_ID); // null, processEnvironmentVariables.MORALIS_MASTER_KEY
    // this._moralis.serverURL = processEnvironmentVariables.MORALIS_SERVER_URL;
    logger.debug("Moralis has been set and initialized");
    return this._moralis;
}

The exception:

Moralis has been set and initialized
ParseError: XMLHttpRequest failed: {"UNSENT":0,"OPENED":1,"HEADERS_RECEIVED":2,"LOADING":3,"DONE":4,"readyState":4,"responseText":"","responseXML":"","status":410,"statusText":null,"withCredentials":false}
    at handleError (C:\Users\<repo>\node_modules\moralis\lib\node\RESTController.js:427:15)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  code: 100
}

When doing it manually over the swagger interface in your website it works fine! I hope you can help. :)

teneko commented 3 years ago
get moralis(): Promise<Moralis> {
    if (this._moralis != null) {
        return this._moralis;
    }
    return this._moralis = new Promise(async (resolve, reject) => {
        try {
            const moralisFromModule = moralis as Moralis; // because moralis/node is not typed
            moralisFromModule.initialize(processEnvironmentVariables.MORALIS_APPLICATION_ID); // processEnvironmentVariables.MORALIS_APPLICATION_ID, null, processEnvironmentVariables.MORALIS_MASTER_KEY
            await moralisFromModule.User.logIn(processEnvironmentVariables.MORALIS_USER_EMAIL, processEnvironmentVariables.MORALIS_USER_PASSWORD);
            // this._moralis.serverURL = processEnvironmentVariables.MORALIS_SERVER_URL;
            logger.debug("Moralis has been set and initialized");
            resolve(moralisFromModule);
        } catch (error) {
            reject(error);
        }
    });
}

After experimenting, log-in does not solve it. I have updated to 0.0.54:

Moralis has been set and initialized
ParseError: XMLHttpRequest failed: {"UNSENT":0,"OPENED":1,"HEADERS_RECEIVED":2,"LOADING":3,"DONE":4,"readyState":4,"responseText":"","responseXML":"","status":410,"statusText":null,"withCredentials":false}
    at handleError (C:\Users\<repo>\node_modules\moralis\lib\node\RESTController.js:427:15)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  code: 100
}

MORALIS_APPLICATION_ID is the copied application-id copied from the swagger interface you can find under https://admin.moralis.io/web3Api.

wondernet commented 3 years ago

I don't know how to run your code.

For this code:

async function x(){
    return this._moralis = new Promise(async (resolve, reject) => {
        try {
            const moralisFromModule = require('moralis/node')
            moralisFromModule.initialize("APP_ID");
            moralisFromModule.serverURL = "SERVER_URL";
            await moralisFromModule.User.logIn("a@b.c", "haha");
            logger.debug("Moralis has been set and initialized");
            resolve(moralisFromModule);
        } catch (error) {
            console.log(error);
            reject(error);
        }
    });
}

x();

It looks like I get an expected Invalid username/password. error;

Capplequoppe commented 3 years ago

MORALIS_APPLICATION_ID is the copied application-id copied from the swagger interface you can find under https://admin.moralis.io/web3Api.

Did you use the default api key as Application id?

teneko commented 3 years ago

MORALIS_APPLICATION_ID is the copied application-id copied from the swagger interface you can find under https://admin.moralis.io/web3Api.

Did you use the default api key as Application id?

Yes. I click on "Copy API Key" and click on "Default". Before that I have taken all the informations from the server you can just create. After the default-API-key approach didn't work out I tried to log-in me first, because I thought the application is maybe scoped to the account and not global. The login itself worked but the exception was just the same: ParseError: XMLHttpRequest.

I don't know how to run your code.

For this code:

async function x(){
    return this._moralis = new Promise(async (resolve, reject) => {
        try {
            const moralisFromModule = require('moralis/node')
            moralisFromModule.initialize("APP_ID");
            moralisFromModule.serverURL = "SERVER_URL";
            await moralisFromModule.User.logIn("a@b.c", "haha");
            logger.debug("Moralis has been set and initialized");
            resolve(moralisFromModule);
        } catch (error) {
            console.log(error);
            reject(error);
        }
    });
}

x();

It looks like I get an expected Invalid username/password. error;

I do not set the server url explicitly when I just take use of the default application id. Can you elaborate what you mean?

ErnoW commented 3 years ago

Is this still an issue? (with the latest version of the SDK and server). I will close this issue, if it is still a problem, please provide the full error and code to replicate it on our forums, as we have more active support over there -> https://forum.moralis.io/