Moralis: Updating Objects

Hello there!!!
I wanna ask that how to access an object and update it… I have found only creating a new object and updating it…

In my case, the trigger is called whenever a new token is created… but later on, I want to change some of its attributes… and unable to do it… used the query but it didn’t work out…
as I am a beginner at blockchain development… so need your help guys…

thanks in advance…

1 Like

Hey @naimaghm :wink:

Take a look at Updating Objects

Please note that if you want to update an object using cloud functions you should use save method

If this does not help, please send the entire code.

3 Likes

Hey @Yomoo …
have tried this already but it’s creating a new object and then update…and I want to update an existing one…
and yes I have read and watched the tutorial but didn’t get my answer… :roll_eyes: :neutral_face:

1 Like

The rewritten docs code bellow works in Cloud functions:

Moralis.Cloud.define("test", async(request) => {
  const { objId } = request.params;
  const query = new Moralis.Query("GameScore");
  query.equalTo("objectId", objId);
  const queryResult = await query.first();
  queryResult.set("score", 1900);
  return queryResult.save()
});

You can find an object by its parameters(for example objId ) and then overwrite the values you need.

3 Likes

Thanks @Yomoo :hugs:

1 Like

@Yomoo is this the best/preferred way to update db object? I’m using nextJS and was planning on just using api/node layer to update object as most of my app is using that layer to fetch from moralis db and other external apis so just thought i’d go client => node/api => Moralis… . Should i be using cloud functions and skipping my api layer in most cases. Seems reduant to use them both but want to make sure i dont’ run into any walls trying to do server side things with node and moralis. (So far it’s been great, but haven’t gotten into anything heavy like Role and ACL yet)

Anyway, thanks for the advice and all your activity here. Also let me know if i should be posting these kinds of more broad philosophical questions elsewhere but seemed related to this so figured i’d just ask.