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.