Cloud Function error: provider.lookups.forEach is not a function

Hi,

Iā€™m trying to write a cloud function which will save the NFTs a user owns to the database when they login, but there seem to be an issue with the function itself. When I run the code locally everything works just fine, but as soon as I try to save the cloud function I got the following error:

2022-02-03T21:44:33.540Z - Cloud Function Error: provider.lookups.forEach is not a function
2022-02-03T21:44:33.539Z - Error initializing plugin  at index 0 provider.lookups.forEach is not a function TypeError: provider.lookups.forEach is not a function
    at /moralis-server/lib/cloud-code/plugins/helpers/chain.js:236:22
    at Array.reduce (<anonymous>)
    at makeProvidersByLookup (/moralis-server/lib/cloud-code/plugins/helpers/chain.js:235:20)
    at loadChainObjects (/moralis-server/lib/cloud-code/plugins/helpers/chain.js:215:40)
    at loadProviders (/moralis-server/lib/cloud-code/plugins/helpers/chain.js:202:3)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async /moralis-server/lib/cloud-code/plugins/index.js:147:11
    at async /moralis-server/lib/cloud-code/plugins/index.js:134:5
    at async /moralis-server/lib/cloud-code/plugins/index.js:134:5
    at async /moralis-server/lib/cloud-code/plugins/index.js:134:5
    at async /moralis-server/lib/cloud-code/plugins/index.js:134:5
    at async /moralis-server/lib/cloud-code/plugins/index.js:134:5
    at async /moralis-server/lib/cloud-code/plugins/index.js:134:5
    at async /moralis-server/lib/cloud-code/plugins/index.js:134:5
    at async /moralis-server/lib/cloud-code/plugins/index.js:134:5
    at async /moralis-server/lib/cloud-code/plugins/index.js:134:5
    at async /moralis-server/lib/cloud-code/plugins/index.js:134:5
    at async /moralis-server/lib/cloud-code/plugins/index.js:134:5
    at async Object.initialize (/moralis-server/lib/cloud-code/plugins/index.js:133:3)

The code looks like:

Moralis.Cloud.define("updateUserNFTs", async (request) => {
    const allOwnedNFTs = request.params.allOwnedNFTs;
    const savedNFTs = request.params.savedNFTs;
    const user = request.user;
  
    const logger = Moralis.Cloud.getLogger();
    logger.info(allOwnedNFTs);
    logger.info(savedNFTs);

    allOwnedNFTs.forEach((NFT) => {
        const unique_id = NFT.token_address.concat(NFT.token_id);
        NFT.unique_id = unique_id;
    });

    savedNFTs.forEach((NFT) => {
        const unique_id = NFT.token_address.concat(NFT.token_id);
        NFT.unique_id = unique_id;
    });

    allOwnedNFTs.forEach((row) => {
        const foundUniqueID = savedNFTs.some(NFT => NFT.unique_id === row.unique_id);
        if (!foundUniqueID) {
            const NFTClass = Moralis.Object.extend("NFT");
            const NFTInstance = new NFTClass();

            NFTInstance.set(row);
            NFTInstance.set("parent", user);
            NFTInstance.save();
        };
    });

    savedNFTs.forEach((row) => {
        const foundUniqueID = allOwnedNFTs.some(NFT => NFT.unique_id === row.unique_id);
        if (!foundUniqueID) {
            const query = new Moralis.Query("NFT");
            query.equalTo("objectId", row.objectId);

            const object = await query.first();

            object.destroy();
        };
    });
});

It seems to be an issue with the forEach() function but strangely enough it does work for another cloud function that I use:

Moralis.Cloud.define("insertUserNFTs", async (request) => {
    const allOwnedNFTs = request.params.NFTs;
    const user = request.user;

    allOwnedNFTs.forEach((NFT) => {
        const NFTClass = Moralis.Object.extend("NFT");
        const NFTInstance = new NFTClass();
        NFTInstance.set(NFT);
        NFTInstance.set("parent", user);
        NFTInstance.save();
    })
});

Any idea what this could be? The server is brand new and up to date.

Thanks in advance!

what is the server url where you get that error?

https://8nlgpqlgs8vy.usemoralis.com:2053/server

1 Like

that specific error should be fixed now on that server, it looks like there is also a syntax error somewhere in the cloud code

Thanks for taking a look! Was this some error on the server? And if so, how can I prevent this when setting up a new one?

As for the syntax error, I was missing an async keyword in one of the forEach() calls.

Everything seems to work now, thanks again!

it was an error related to the server, and that error should not happen again for a new created server

1 Like