Attempting to Run node addEvents.js

I am trying to test my catches on the Moralis network but when I attempt to run the " node addEvents.js" I run into this error

This is my addEvents.js file

// ItemListed
// ItemBought
// ItemCanceled

const Moralis = require("moralis/node")
require("dotenv").config()
const contractAddresses = require("./constants/networkMapping.json")

Moralis.start({
    appId: "0uqP7OPfh8fxjCnQcBjrZzqpStRQ5rzwMz8gsY1N",
})

let chainId = process.env.chainId || 31337
let moralisChainId = chainId == "31337" ? "1337" : chainId

const contractAddress = contractAddresses[chainId]["NftMarketplace"][0]
const serverUrl = process.env.NEXT_PUBLIC_SERVER_URL
const appId = process.env.NEXT_PUBLIC_APP_ID
const masterkey = process.env.masterKey

async function main() {
    await Moralis.start({ serverUrl, appId, masterkey })
    console.log(`Working with contract address ${contractAddress}`)

    let itemListedOptions = {
        // Moralis understands a local chain is 1337
        chainId: moralisChainId,
        sync_historical: true,
        topic: "ItemListed(address,address,uint256,uint256)",
        abi: {
            anonymous: false,
            inputs: [
                {
                    indexed: true,
                    internalType: "address",
                    name: "seller",
                    type: "address",
                },
                {
                    indexed: true,
                    internalType: "address",
                    name: "nftAddress",
                    type: "address",
                },
                {
                    indexed: true,
                    internalType: "uint256",
                    name: "tokenId",
                    type: "uint256",
                },
                {
                    indexed: false,
                    internalType: "uint256",
                    name: "price",
                    type: "uint256",
                },
            ],
            name: "ItemListed",
            type: "event",
        },
        tableName: "ItemListed",
    }
    let itemBoughtOptions = {
        chainId: moralisChainId,
        sync_historical: true,
        topic: "ItemBought(address,address,uint256,uint256)",
        abi: {
            anonymous: false,
            inputs: [
                {
                    indexed: true,
                    internalType: "address",
                    name: "buyer",
                    type: "address",
                },
                {
                    indexed: true,
                    internalType: "address",
                    name: "nftAddress",
                    type: "address",
                },
                {
                    indexed: true,
                    internalType: "uint256",
                    name: "tokenId",
                    type: "uint256",
                },
                {
                    indexed: false,
                    internalType: "uint256",
                    name: "price",
                    type: "uint256",
                },
            ],
            name: "ItemBought",
            type: "event",
        },
        tableName: "ItemBought",
    }

    let itemCanceledOptions = {
        chainId: moralisChainId,
        sync_historical: true,
        topic: "ItemCanceled(address, address,uint256)",
        abi: {
            anonymous: false,
            inputs: [
                {
                    indexed: true,
                    internalType: "address",
                    name: "seller",
                    type: "address",
                },
                {
                    indexed: true,
                    internalType: "address",
                    name: "nftAddress",
                    type: "address",
                },
                {
                    indexed: false,
                    internalType: "uint256",
                    name: "tokenId",
                    type: "uint256",
                },
            ],
            name: "ItemCanceled",
            type: "event",
        },
        tableName: "ItemCanceled",
    }
    const listedResponse = await Moralis.Cloud.run("watchContractEvent", itemListedOptions, {
        useMasterKey: true,
    })
    const boughtResponse = await Moralis.Cloud.run("watchContractEvent", itemBoughtOptions, {
        useMasterKey: true,
    })
    const canceledResponse = await Moralis.Cloud.run("watchContractEvent", itemCanceledOptions, {
        useMasterKey: true,
    })
    if (listedResponse.successs && canceledResponse.success && boughtRepsonse.success) {
        console.log("Success! Database Updated with wathcing events")
    } else {
        console.log("Something went wrong...")
    }
}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error)
        process.exit(1)
    })

I didnt originally have the Moralis.Start, i added when trying out different methods to fix the error

Make sure to remove that top Moralis.start().

Check this environment variable process.env.NEXT_PUBLIC_APP_ID in your .env file and make sure it’s valid (try console.log with it in this script), it seems it is undefined in your await Moralis.start({ serverUrl, appId, masterkey }).

Also masterkey should be masterKey.

so i changed the masterkey to masterKey, and i removed the above Moralis.start
how do i console.log it in the script

You can add this anywhere e.g. in your main function.

console.log(process.env.NEXT_PUBLIC_APP_ID)

If it seems valid, test by hardcoding all of your server values.

okay so i had a space in between the _ and APP and it got a litte further then i ran into this error
Capture5

You will need to debug this script to see where it’s failing, you can console.log your responses from your cloud functions.

so i added
console.log(listedResponse)
console.log(boughtResponse)
console.log(canceledResponse)
Capture7

i solved it the issue was the tablename