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

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?

Still it is same. Moralis SDK connection error. Given the error at the top.

I got everything working fine, using:

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

as suggested by Rick, you need to add moralisSecret, but do not remove the other params otherwhise the other functions won’t work anymore !

I wonder why this is not in the doc, and it is really hard to debug from the assert !

2 Likes
await Moralis.start({ serverUrl, appId, masterKey, moralisSecret });
await Moralis.enableWeb3({
  chainId: 0x61,
  privateKey: 'priate_key',
});

This works previously with me but now it is not working. I am getting the same error "window is not defined’. I have changed the chain to BSC testnet and Mumbai testnet (changed chain id too). But still it not working. Please let me know how to work with it now.

This stopped working since Moralis discontinued speedy nodes. You can use web3js instead