Object Not Found on Update

Hello,

I am having a bad time with CloudFunctions and updating an item that exists in the database.

I am following this example of querying an object based on an attribute and then setting a different attribute, followed by a save. The field i want to update is called ‘theUpdateValue’ and is a boolean which is initially False. I can see this column in the database dashboard.

Example taken from - Moralis: Updating Objects

Moralis.Cloud.define("updateMyObject", async (request) => {
  const AirdropQualifier = Moralis.Object.extend("MyObject");
  const logger = Moralis.Cloud.getLogger();
  const query = new Moralis.Query(AirdropQualifier);
  try{
      const address = request.params.address;
      query.equalTo("address", address);
      const queryResult = await query.first();
      logger.info(address);   <------ THIS IS CORRECT AND LOGS TO CONSOLE
      logger.info(queryResult.id);     <-------- THIS IS CORRECT AND LOGS TO CONSOLE
      queryResult.set("theUpdateValue", request.params.value);
      return queryResult.save();
  } catch(err){
      return err
  }
});

I am getting. What am i doing wrong here?

2022-03-23T10:44:33.827Z - Error: Object not found.

When is use logger.info(JSON.stringify(result)) i can see the correct result in the dashboard as per the following post.

Full error from log

2022-03-23T12:23:42.977Z - Error: Object not found.
at /moralis-server/lib/Controllers/DatabaseController.js:615:17
at processTicksAndRejections (node:internal/process/task_queues:96:5)

you can try .save(null,{useMasterKey:true});

https://docs.moralis.io/moralis-server/cloud-code/cloud-functions#using-the-master-key-in-cloud-code

@cryptokid Thank you for your reply.

This does not fix the issue. Is there anything else i can try?

you can try await for that .save

Yup. Tried that already…

  const queryResult = await query.first();
  queryResult.set("theUpdateValue", request.params.canClaim);
  return await queryResult.save(null,{useMasterKey:true})

you can try .find instead of .first and then to get the first element, it shouldn’t be a difference

That was the first approach that i tried. Then i found Moralis: Updating Objects which used .first()

The table has many rows. Like 23k, could that be related?

what is the server url?
it shouldn’t be a problem with 23k rows

https://7bom7bqipiks.usemoralis.com:2053/server

does it work with other tables or it never worked?

I have not needed to update other tables. I can try i guess.

It has never worked, this is my first time implementing it.

List/Get/Create all seem to work fine. It is only the update that is letting me down.

This table was populated with Direct Database access. That could be another difference.

https://docs.moralis.io/moralis-server/database/direct_access


import json
import pymongo

MONGO_HOST = "2342"
MONGO_PORT = 56728

con = pymongo.MongoClient(MONGO_HOST, MONGO_PORT)
qualifier_table = con['parse']['AirdropQualifier']

with open(f"qualifier_data.json","r") as json_file:
    white_list = json.load(json_file)

qualifier_table.insert_many(white_list)

The createdAt and updatedAt fields are (undefined)

I saw that the table seems to have strange data, like no createdAt or updatedAt, and the objectId seems strange, maybe that is the problem

I’ll try adding those fields when i populate the database. This is starting to make sense!

It’s looking better in the tables, but still the same error.

can you try not to set the objectId and maybe it creates automatically an object id?

That’s what we did originally.

I tried again, setting only createdAt and updatedAt and it now has duplicated ids.

I’ll try doing it manually via the Moralis SDK but with 23k rows it is not ideal.

where is bulk write too

createdAt and updatedAt shouldn’t be the problem, but that ID didn’t look like an id with the same format as in the other tables