[SOLVED] NodeJS serverside Moralis error (window is not defined)

Hi everyone,

Does someone know how to execute contract functions with Moralis on a NodeJS app?

I got the following error:

(node:14564) UnhandledPromiseRejectionWarning: ReferenceError: window is not defined
    at InjectedWeb3Connector.verifyEthereumBrowser (/root/nft_generator/node_modules/moralis/lib/node/Web3Connector/InjectedWeb3Connector.js:66:5)
    at InjectedWeb3Connector.activate (/root/nft_generator/node_modules/moralis/lib/node/Web3Connector/InjectedWeb3Connector.js:72:10)
    at InternalWeb3Provider.activate (/root/nft_generator/node_modules/moralis/lib/node/InternalWeb3Provider.js:65:30)
    at Function.enableWeb3 (/root/nft_generator/node_modules/moralis/lib/node/MoralisWeb3.js:215:45)
    at executeContract (/root/nft_generator/index.js:112:23)
    at process._tickCallback (internal/process/next_tick.js:68:7)

My code looks like:

const executeContract = async () => {
       await Moralis.start({
            serverUrl: serverUrl,
            appId: appId,
            masterKey: masterKey
        });

        // Enable web3
        await Moralis.enableWeb3({
            chainId: 0x80001,
            privateKey: walletPrvKey,
        });

        const options = {
        ....
        }

        await Moralis.executeFunction(options).then((result) => {
            console.log(result);
        });
}

For additional information, I am using the following libraries: Axios, Moralis, and Express:

// Require .env config settings
require('dotenv').config();

// Include libraries
const axios = require('axios');
const Moralis = require("moralis/node");
const express = require('express');
1 Like

For anyone who is searching for this problem:

I had to change:

await Moralis.start({
   serverUrl: serverUrl,
   appId: appId,
   masterKey: masterKey
});

To:

await Moralis.start({
   moralisSecret: moralisSecret
});

You can find your MoralisSecret in you account setting https://admin.moralis.io/servers (in the right top corner).

1 Like

But if I use this “await Moralis.start({ moralisSecret: moralisSecret })”. It solves the window is not defined error but I cannot call the read functions like: “const owenrNft = await Moralis.Web3API.native.runContractFunction(options)”. It gives another error which is “Error: ABI is required! at Function.fetchFromApi”. Please help me out of it.

how did you import Moralis SDK?

Here is my codes:

let serverUrl = process.env.MORALIS_SERVER_URL;
let appId = process.env.MORALIS_APP_ID;
let masterKey = process.env.MORALIS_MASTER_KEY;

const moralisServer = async () => {
await Moralis.start({ serverUrl, appId, masterKey });
logger.info(“Moralis connected”);
};

moralisServer();

This works perfectly to call read funcitons like symbol “ERC721MockContract”. But if I use the code below:
let moralisSecret = process.env.MORALIS_SECRET;
const moralisServer = async () => {
await Moralis.start({ moralisSecret });
await Moralis.enableWeb3({
privateKey: process.env.PRIVATE_KEY2,
});
};
moralisServer();

It shows error: ABI is required!. Here is my full code:

const options = {
chain: “bsc testnet”,
address: “0x0252906b8835fBDB4b8E579db8641F1EC38b6D23”,
function_name: “ownerOf”,
abi: ABI,
params: {
tokenId: tokenId.tokenId,
},
};

const owenrOfNft = await Moralis.Web3API.native.runContractFunction(options);

this code doesn’t include the part where Moralis SDK is imported

I have imported it in index.js file from where the server is running.

still, how did you import it, what is the syntax that you used?

If I imported it in a wrong way then it shouldn’t be work with “await Moralis.start({ serverUrl, appId, masterKey })”. I guess.

there are different ways to import it, you can import the browser version or the server version

const Moralis = require(“moralis/node”);
const moralisServer = async () => {
await Moralis.start({ serverUrl, appId, masterKey });
logger.info(“Moralis connected”);
};

It works perfectly with the read functions like “symbol” (Moralis.Web3API.native.runContractFunction). But doesn’t work with any functions where I use Moralis.executeFunction.

And this code doesn’t work with read functions. and gives that error.
const Moralis = require(“moralis/node”);
const moralisServer = async () => {
await Moralis.start({ moralisSecret });
await Moralis.enableWeb3({
privateKey: process.env.PRIVATE_KEY2,
});
}

Moralis.executeFunction requires that private key

did you try with latest version of Moralis SDK?
I know that Moralis.transfer works in some particular circumstances.

No. I didn’t try with the latest I guess. Please suggest me how to work with that.

you install latest version of the sdk, you can check with Moralis.CoreManager.get("VERSION") what version is running now.

latest version now is 1.7.0

Yes. I am using “js1.7.0” version. :sob:

in this case, for now, you can use web3 or ethers syntax directly to execute a function with private key

Please give me a sample code of it. It will be helpful for me. Thanks in advance.

you could read on this thread:

Still. I couldn’t solve the problem. :sob:

What error(s) do you get? Or is it still the same one?