[SOLVED] Smart contract event handling- issue with beforeSave trigger

Hi,
I’m trying to create a very basic cloud function trigger for my smart contract event, but it’s not working.
Just for testing, I trimmed everything, and kept the basic functionality, and it still does not work, not sure where exactly is the issue here:

Cloud function code:

Moralis.Cloud.beforeSave("EQuestionStatusNEW", (request) => {
    const Question = Moralis.Object.extend("EQuestionNEW");
    const query = new Moralis.Query(Question);
    query.equalTo("questionId", 2);
    const question = await query.first();
    question.set("questionStatusId", 1);    // this is not updated in collection
    question.save();    
});

EQuestionStatusNEW & EQuestionNEW are the collection names.

Can you please help resolve this.

1 Like

Hey @vikasg

Looks like you are trying to find a record in the database before it was made.

question is probably undefined

You can use afterSave trigger instead. Or you can edit your code a bit for using it with beforeSave":

You need to get info from request object, because the record you are trying to get doesn’t exist in the db yet.

Here how should your code look like:

Moralis.Cloud.beforeSave("EQuestionStatusNEW", async (request) => {
    const questionId = request.object.get("questionId");
    question.set("questionStatusId", 1);    // this is not updated in collection
    await question.save();    
});

Hope this helps :man_mechanic:

1 Like

Thanks @Yomoo for your reply.

Makes sense to use afterSave trigger here, will do that.

Also, EQuestionNEW is a different collection altogether, and it has entry with questionId = 2. The requirement here is to update a different collection (EQuestionNEW) once I receive event from smart contract.

    const Question = Moralis.Object.extend("EQuestionNEW");
    const query = new Moralis.Query(Question);
    query.equalTo("questionId", 2);
    const question = await query.first();

I saw this await was missing from my code earlier, but even after putting this, its not updating the EQuestionNEW collection.
await question.save();

So final expected code should look like this:

Moralis.Cloud.afterSave("EQuestionStatusNEW", (request) => {
    const questionId =  request.object.get("questionId");
    const questionStatusId =  request.object.get("questionStatusId");
    
    const Question = Moralis.Object.extend("EQuestionNEW");
    const query = new Moralis.Query(Question);
    query.equalTo("questionId", questionId);
    const question = await query.first();
    
    question.set("questionStatusId", questionStatusId);
    await question.save();    
});

you could check if a question object was found there
it will not work if no object is found
you can also try to use master key in case that it is needed

Object is there. I also checked Moralis admin log, strange error there:

2022-01-08T11:24:05.517Z - TypeError: Cannot read properties of undefined (reading 'set')
    at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:140:21), <anonymous>:1:213)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-01-08T11:24:05.515Z - afterSave failed for EQuestionStatusNEW for user undefined:
  Input: {"block_timestamp":{"__type":"Date","iso":"2022-01-08T11:24:04.000Z"},"transaction_hash":"0x3589f438656633f521352f786461f22654995a5837d660da6395ff8e3c7bfca5","log_index":0,"questionId":"4","questionStatusId":"1","address":"0x2a0ea93f4979ddaf532ac65ec5345e4cc4828fbf","block_hash":"0x24e2dcd692591f48408a259d96223c8cb5eefc39798320c592fe3cc581fdf96b","block_number":15682380,"transaction_index":0,"createdAt":"2022-01-08T11:24:05.490Z","updatedAt":"2022-01-08T11:24:05.490Z","objectId":"EaYkbechbyrimaFOmglJxdeC"}
  Error: {"message":"Cannot read properties of undefined (reading 'set')","code":141}

Could not understand the error:

  Error: {"message":"Cannot read properties of undefined (reading 'set')","code":141}

This column questionStatusId do exists in EQuestionNEW collection.
Any quick help would be appreciated, seems I’m stuck for a while now.

you can check if that query returned an object, it looks like it returns undefined from that error

Its resolved now. The last issue was related to collection column datatype.
Thanks everyone for your help. Really appreciate it!

1 Like