[SOLVED] Moralis Cloud Functions Not Logging

Hey guys,

Here is my cloud function:

/******/ (() => { // webpackBootstrap
2/******/ 	var __webpack_modules__ = ({
3
4/***/ 227:
5/***/ ((module) => {
6
7module.exports = eval("require")("moralis/types");
8
9
10/***/ })
11
12/******/ 	});
13/************************************************************************/
14/******/ 	// The module cache
15/******/ 	var __webpack_module_cache__ = {};
16/******/ 	
17/******/ 	// The require function
18/******/ 	function __nccwpck_require__(moduleId) {
19/******/ 		// Check if module is in cache
20/******/ 		var cachedModule = __webpack_module_cache__[moduleId];
21/******/ 		if (cachedModule !== undefined) {
22/******/ 			return cachedModule.exports;
23/******/ 		}
24/******/ 		// Create a new module (and put it into the cache)
25/******/ 		var module = __webpack_module_cache__[moduleId] = {
26/******/ 			// no module.id needed
27/******/ 			// no module.loaded needed
28/******/ 			exports: {}
29/******/ 		};
30/******/ 	
31/******/ 		// Execute the module function
32/******/ 		var threw = true;
33/******/ 		try {
34/******/ 			__webpack_modules__[moduleId](module, module.exports, __nccwpck_require__);
35/******/ 			threw = false;
36/******/ 		} finally {
37/******/ 			if(threw) delete __webpack_module_cache__[moduleId];
38/******/ 		}
39/******/ 	
40/******/ 		// Return the exports of the module
41/******/ 		return module.exports;
42/******/ 	}
43/******/ 	
44/************************************************************************/
45/******/ 	/* webpack/runtime/compat */
46/******/ 	
47/******/ 	if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
48/******/ 	
49/************************************************************************/
50var __webpack_exports__ = {};
51// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
52(() => {
53const { default: Moralis } = __nccwpck_require__(227);
54
55//add Whoopy to active whoopys once created  
56//start listening to active whoopys for events 
57//remove from active whoopys 24 hours after winners picked 
58
59
60Moralis.Cloud.afterSave("WhoopyCreated", async (request) => {
61    const confirmed = request.object.get("confirmed")
62    const logger = Moralis.Cloud.getLogger()
63    logger.info("Looking for confirmed tx")
64})
65
66})();
67
68module.exports = __webpack_exports__;
69/******/ })()
70;

However, for some reason, my logs are logger.info is not working. I am able to save the event successfully and it is showing up in my Database, so I can’t figure out what the issue is. Any help would be appreciated. Thanks!

PS: Server url is: https://mgjfj1lq8sbh.usemoralis.com:2053/server

afterSave triggers only for real time events and not also for historical sync

You could test it by trying to change a row directly in the dashboard interface

WhoopyCreated is a realtime event. Here is the code:

const Moralis = require("moralis/node")
require("dotenv").config()
const whoopyContractAddresses = require("./constants/whoopyContractAddresses.json")
const wfContractAddresses = require("./constants/wfContractAddresses.json")

let chainId = process.env.chainId || 31337
let moralisChainId = chainId == "31337" ? "1337" : chainId
const serverUrl = process.env.NEXT_PUBLIC_SERVER_URL
const appId = process.env.NEXT_PUBLIC_APP_ID
const masterKey = process.env.masterKey

const contractAddress = wfContractAddresses[chainId][0]

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

    let whoopyCreated = {
        chainId: moralisChainId,
        address: contractAddress,
        topic: "NewClone(address,address)",
        sync_historical: true,
        abi: {
            anonymous: false,
            inputs: [
              {
                indexed: true,
                internalType: "address",
                name: "instance",
                type: "address"
              },
              {
                indexed: true,
                internalType: "address",
                name: "creator",
                type: "address"
              }
            ],
            name: "NewClone",
            type: "event"
          },
        tableName: "WhoopyCreated"
    }

    const listedResponse = await Moralis.Cloud.run("watchContractEvent", whoopyCreated, {useMasterKey: true})
    if (listedResponse.success) {
        console.log("Success! Database Updated!")
    } else {
        console.log("Something went wrong...")
    }
 

}

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

This code is adding an event sync. It doesn’t tell if that event is real-time or not.

How can I test it by directly changing a row in the interface? My aim is to be able to dynamically create a new class upon the event getting created.

Also, for reference, I am following Patrick’s tutorial right now (many on this forum have followed as well), and his database shows addressSyncStatus and mine is eventSyncStatus. Do you think the issue has something to do with that, and if so, how can I fix it?

Those two tables are two different tables. There is no issue with that. When you edit a row in the database dashboard then the afterSave hook should trigger and you should see that logged line.

Just tried editiing a line in the dashboard and didn’t update. Also, in the tutorial, it happens automatically upon running the script. So in my case, when I call the particular function, it emits the event (which is saved in the ‘WhoopyCreated’ table.

As per Moralis docs, afterSave should trigger when this happens automatically, however that is not the case (I apologise for the multiple questions or if I’m missing something)

What it didn’t update?

Yes, afterSave should trigger when a row is updated in the dashboard or when data is added in that table with real time sync (equivalent of making a new transaction)

Yeah didn’t update. What exactly is real-time sync and how do I make it real time sync?

Real time sync only refers to future events and not for events form the past. You have to make a new transaction in order to generate a new event.

Yep, I’m making new transactions which are being recorded on the dashboard, yet not showing up in logs. Is there a way for me to do this another way?

Then it has to be something with that hook. Try beforeSave, try to upload the cloud code again

Hey @cryptokid ,
I figured it out. For some reason VSC had imported this:

const { default: Moralis } = require("moralis/types");

And this was causing an error in the cloud code. Removed it and it worked! (I knew it was going to be something stupid.

Regardless, once again, thanks for the quick replies and thanks for bearing with me!

1 Like