[SOLVED] Cloud function triggers but do not create a record

Hi !

We are experiencing a strange issue about exact same CloudFunction code does not have the same effect on two different database classes (only the class name differs).

It’s happening on a Mumbai server (https://5o8m7xjcginv.usemoralis.com:2053/server) where we run some cloud functions that use the beforeSave hook to create a row on a different class.

The code looks something like this:

Moralis.Cloud.beforeSave("MyClass", async (request) => {
    const MyClass2 = Moralis.Object.extend("MyClass2");

    const query = new Moralis.Query(MyClass2);
    query.equalTo(
        "request_transaction_hash",
        request.object.get("transaction_hash")
    );

    const retrievedStatusRecord = await query.first({ useMasterKey: true });
    if (retrievedStatusRecord !== undefined) {
        // Transactions might come at any point, already confirmed, unconfirmed, or first unconfirmed and then confirmed
        // We only want to handle the event once, so we are using the transaction hash as a unique identifier
        return;
    }

    // Create status record
    const statusRecord = new MyClass2();
    statusRecord.set(
        "request_transaction_hash",
        request.object.get("transaction_hash")
     );
    statusRecord.set("message", "lorem ipsum");
    statusRecord.save(null, { useMasterKey: true });

    // Do some HTTP request
    Moralis.Cloud.httpRequest({ ... });
});

The odd thing is that for some classes it all works as expected, and for other it doesn’t. The HTTP request fires in both cases but the MyClass2 row never gets created. There are no error logs or anything at the time of request.

The only error I can see on the server is an error message MongoError: cannot infer query fields to set, path 'log_index' is matched twice that appears like once every 24 hours or so. Not sure what it is about but it does not seem related.

We were not calling .then on the HTTP request promise but I have tried doing it and suggested on the example and logging the answer and everything is fine on that side…

Is there maybe a limit in how long classes can be named ? Character count I mean.

Let me know if you have any ideas what this could be about !

Thank you very much for you help

Which specific class(es) doesn’t work? Is it consistent for every beforeSave that runs on this class? Add a catch block to your statusRecord.save to make sure there’s no errors.

We were not calling .then on the HTTP request promise

Is the HTTP request related to the object being saved?

Yes, it is consistent. For example, beforeSave triggers on RequestedBallBuy will always be able to create RequestedBallBuyStatus records, but beforeSave triggers on RequestedZomonLevelSync will never be able to create RequestedZomonLevelSyncStatus records. As far as I can tell, both xxxStatus classes have the same columns and the cloud function code is the same just changing the target class name.

I didn’t know there was a catch block, same for the HTTP network actually, I guess that’s new. I will try and let you know :+1:

No, as you can see in the example, the record is saved first and then the request is triggered to notify a different system. The outcome of the request is not used for anything.

Thanks for the tip @alex. Adding a few catch on the save calls I was able to trace down the issue to a required string column where I am initially setting "" (empty string) and the DB is complaining about it => "report_transaction_hash is required".

I looked upon the required columns on other tables and it is indeed the issue. I guess I will need to write down this in our internal documentation so it doesn’t happen again :grinning_face_with_smiling_eyes:

Thank your for your help :+1: