Can't update an object's property using objectId

I’ve just made some small adjustments to your code and ran it in the parse js console to show you that this works.

I wouldn’t set the values in the then call…it doesn’t really make sense. Also, if you really want to be sure that the value has been set in the db, you’d need to query again, else you’re just working with the in memory object.

So, the steps are:

  • query to get the object you want
  • make the changes to the object
  • await the save. If you wrap yoru save in a try/catch, you’ll know if it has saved successfully and can work with the set value as you need to, without the need to refetch

Here’s the code:

let Test = Parse.Object.extend("Test");
let query = new Parse.Query("Test");
query.equalTo("objectId", "IfUw8oQcgX");
let test = await query.first() // this returns a single item
// let test = await query.find() // this returns an array of results

console.log('Intial name:')
console.log(test.get("name"));

// change the value of `name`
test.set("name", "Not a random name");
await test.save();

// make a new query to test what you've done
let queryTwo = new Parse.Query("Test");
queryTwo.equalTo("objectId", "IfUw8oQcgX");
let testTwo = await queryTwo.first()

// this should contain the updated value (i.e. "Not a random name")
console.log('Updated name:')
console.log(testTwo.get("name")); 

I hope this is helpful in some way, and apologies if I’ve misunderstood you.

1 Like