[SOLVED]Cloud function not working

Hi,

I’ve written two cloud functions to be executed afterSave, one of them is working (it registers the listing of a newly minted NFT and adds it to a new list of ActiveNFT) and the other one, the function that should delete the NFT from ActiveNFT is not working at all.

I’ve checked my code and it’s running fine, as I get the correct error message when trying to delete an NFT already deleted.

I’ve waited and refreshed the page, in case that was a matter of execution time, but the NFT are still in the ActiveNFT list.

here is my cloud functions document:

/******/ (() => { // webpackBootstrap
/******/ 	/* webpack/runtime/compat */
/******/ 	
/******/ 	if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/ 	
/************************************************************************/
var __webpack_exports__ = {};
// script that will create a list of Active Items, delete them once they are bought

Moralis.Cloud.afterSave("NFTListed", async (request) => {
  // every event gets triggered 2x, once on unconfirmed, once on confirmed
  const confirmed = request.object.get("confirmed")
  const logger = Moralis.Cloud.getLogger()
  logger.info("Looking for confirmed transaction")
  const ActiveNFT = Moralis.Object.extend("ActiveNFT")
  if (confirmed) {
    logger.info("NFT Found!")
    //create a new table:
    const query = new Moralis.Query(ActiveNFT)
    query.equalTo("nftAddress", request.object.get("nftAddress"))
    query.equalTo("tokenId", request.object.get("tokenId"))
    query.equalTo("marsketplaceAddress", request.object.get("address"))
    query.equalTo("seller", request.object.get("seller"))
    logger.info(`Marsketplace | Query: ${query}`)
    const alreadyListedNFT = await query.first()
    console.log(`alreadyListedItem ${JSON.stringify(alreadyListedNFT)}`)
    if (alreadyListedNFT) {
      logger.info(`Deleting ${alreadyListedNFT.id}`)
      await alreadyListedNFT.destroy()
      logger.info(
        `Deleted item with tokenId ${request.object.get(
          "tokenId"
        )} at address ${request.object.get("address")} since the listing is being updated. `
      )
    }
  }

  //create a new entry in the table:
  const activeNFT = new ActiveNFT()
  // set columns in the entry:
  activeNFT.set("marsketplaceAddress", request.object.get("address"))
  activeNFT.set("nftAddress", request.object.get("nftAddress"))
  activeNFT.set("price", request.object.get("price"))
  activeNFT.set("tokenId", request.object.get("tokenId"))
  activeNFT.set("seller", request.object.get("seller"))
  logger.info(
    `Adding address: ${request.object.get("address")}, TokenId: ${request.object.get("tokenId")}}`
  )
  logger.info("Saving...")
  await activeNFT.save()
})

Moralis.Cloud.afterSave("NFTDeleted", async (request) => {
  const confirmed = request.object.get("confirmed", { useMasterKey: true })
  const logger = Moralis.Cloud.getLogger()
  logger.info(`Marsketplace | Object: ${request.object}`)
  if (confirmed) {
    logger.info("NFT Deleted!")
    //create a new table:
    const ActiveNFT = Moralis.Object.extend("ActiveNFT")
    const query = new Moralis.Query(ActiveNFT)
    query.equalTo("marsketplaceAddress", request.object.get("address"))
    query.equalTo("nftAddress", request.object.get("nftAddress"))
    query.equalTo("tokenId", request.object.get("tokenId"))
    logger.info(`Marsketplace | Query: ${query}`)
    const deletedNFT = await query.first()
    logger.info(`Marsketplace | CanceledNFT: ${deletedNFT}`)
    if (deletedNFT) {
      await deletedNFT.destroy()
      logger.info(
        `Deleting ${request.object.get("tokenId")} at address ${request.object.get(
          "address"
        )} since it was canceled.`
      )
    } else {
      logger.info(
        `No NFT found at the address ${request.object.get(
          "address"
        )} and tokenID ${request.object.get("tokenId")}`
      )
    }
  }
})


module.exports = __webpack_exports__;
/******/ })()
;

and here are the logs:

2022-09-15T07:49:47.035Z - Saving...
2022-09-15T07:49:47.035Z - Adding address: 0xe7f1725e7734ce288f8367e1bb143e90bb3f0512, TokenId: 0}
2022-09-15T07:49:47.033Z - Deleted item with tokenId 0 at address 0xe7f1725e7734ce288f8367e1bb143e90bb3f0512 since the listing is being updated. 
2022-09-15T07:49:47.013Z - Deleting MY8RCaL82Fs6IWILfEkrARIN
2022-09-15T07:49:46.991Z - Marsketplace | Query: [object Object]
2022-09-15T07:49:46.990Z - NFT Found!
2022-09-15T07:49:46.989Z - Looking for confirmed transaction
2022-09-15T07:49:46.410Z - Saving...
2022-09-15T07:49:46.409Z - Adding address: 0xe7f1725e7734ce288f8367e1bb143e90bb3f0512, TokenId: 0}
2022-09-15T07:49:46.407Z - Looking for confirmed transaction

On the other hand, the first function deletes an existing NFT from the list because I cannot seem to reset the local Devchain, no matter what browser I use.

you could use JSON.stringify for that object to see for what object gets triggered

Ok so I see what’s going on, I read my logs wrong, the second function is not running at all, it’s the fact that the server finds the tokenID to already exist. I think it comes from the fact that every time I hit the Reset local Devchain, on the dashboard, nothing happens.

That does not explain why the cancel script is not working though :confused:
Here are the logs with JSON stringify :

2022-09-15T12:11:49.760Z - Saving...
2022-09-15T12:15:36.985Z - Saving...
2022-09-15T12:15:36.985Z - Adding address: 0xe7f1725e7734ce288f8367e1bb143e90bb3f0512, TokenId: 2}
2022-09-15T12:15:36.984Z - Deleted item with tokenId 2 at address 0xe7f1725e7734ce288f8367e1bb143e90bb3f0512 since the listing is being updated. 
2022-09-15T12:15:36.961Z - Deleting wPaIjcO5oVyNaZW7Dq9o2PtY
2022-09-15T12:15:36.960Z - alreadyListedItem {"marsketplaceAddress":"0xe7f1725e7734ce288f8367e1bb143e90bb3f0512","nftAddress":"0x5fbdb2315678afecb367f032d93f642f64180aa3","price":"1000000000000000000","tokenId":"2","seller":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266","createdAt":"2022-09-15T12:15:36.754Z","updatedAt":"2022-09-15T12:15:36.754Z","objectId":"wPaIjcO5oVyNaZW7Dq9o2PtY"}
2022-09-15T12:15:36.938Z - Marsketplace | Query: {"where":{"nftAddress":"0x5fbdb2315678afecb367f032d93f642f64180aa3","tokenId":"2","marsketplaceAddress":"0xe7f1725e7734ce288f8367e1bb143e90bb3f0512","seller":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"}}
2022-09-15T12:15:36.937Z - NFT Found!
2022-09-15T12:15:36.936Z - Looking for confirmed transaction
2022-09-15T12:15:36.751Z - Saving...
2022-09-15T12:15:36.750Z - Adding address: 0xe7f1725e7734ce288f8367e1bb143e90bb3f0512, TokenId: 2}
2022-09-15T12:15:36.748Z - Looking for confirmed transaction

Deleting the server and creating a new one seems to have solved the issue, the second cloud function is now running.

1 Like