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.