Can't update an object's property using objectId

I’ve been trying to update an object’s property inside a React app (using npm module: parse), but I can’t do that. I’ve tried several different methods, but none of those seem to work out. Here are a few snippets that I tried:

const query = new Parse.Query(tableName);
query.equalTo('id', objectId);
await query.get('123abc', {
	success: function (object) {
		console.log('object', object);
		object.set('name', 'john doe');
		object.save();
	},
	error: function (error) {
		console.log('error', error);
		alert("Error: " + error.code + " " + error.message);
	}
});


const ParseObj = Parse.Object.extend(tableName);
let instance = new ParseObj();

instance.id = '123abc';
instance.set('name', 'john doe');
const result = await instance.save();
console.log('result', result);


const ParseObj = Parse.Object.extend(tableName);
const query = new Parse.Query(ParseObj);
query.get('123abc').then(
	(obj) => {
		console.log('obj', obj);
	},
	(error) => {
		console.log('error', error);
	}
);

Also, here are some of the few articles I used reference from (can’t put more than 2 links due to new account):

Hi @mayank-droid,

Are you saying that you want to look an item up, change some values and then save it?

If so, it’s pretty straight forward. Let’s say you have a collection called Media, which contains a bunch of entries and you want to set the name of a specific entry. This is how you could do that:

// First we get the specific item that we'd like to update and store it in a variable
const media = await new Parse.Query('Media')
    .equalTo('objectId', updatedObj.objectId) // you could search by other values too
    .first()

    media.set('name', updatedObj.name)

    // now call save on the object and it will be updated.
    await media.save()

The docs are pretty great at explaining this sort of stuff too: JavaScript Developers Guide | Parse

If I’ve misunderstood you, please describe in a bit more detail what you’re trying to do.

1 Like

Thanks @woutercouvaras for the solution. Unfortunately, it didn’t work. I’m still getting the following error:
Screenshot 2022-12-14 at 11.00.17 AM

Although when I try this command with the same class name, I get all the results:
Screenshot 2022-12-14 at 11.08.05 AM

From that error message, it looks like the variable tableName is not set. Are those blocks of code in the same scope? I.e. is tableName definitely available to both of those pieces of code?

Perhaps, just to make debugging easier, try typing the string name of your collection/table in place of the variable.

Yes, all of the code is in same scope. In fact, I’m using variables just to conceal the real values. But inside the code, I’m actual values in their raw form. Also, the value for tableName (className in this context) is a totally valid string.

Okay, cool.

Could you share the exact code that gave you the error earlier? It’s hard to help without the context.

That crud stuff should just work, so there’s probably something in your code that a little off.

Sure, here it is:

async function run() {
  let Test = Parse.Object.extend("Test");
  let query = new Parse.Query(Test);
  let test = await query.get("OoJdRQFV5H");

  console.info(test.get("name"));
  console.info(test.get("username"));

  test.save().then((test) => {
    test.set("name", "Not a random name");

    console.info(test.get("name"));
    console.info(test.get("username"));

    return test.save();
  });
}

run(); 

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

Hi @woutercouvaras, thanks for trying so much to help. I really appreciate it. Unfortunately, I’m getting empty result even from the first query when equalTo is applied with an objectId. But surprisingly, that objectId (checked cases and everything) exists in the array when I just run find method on it (without equalTo).

Have you got any ACLs set on that specific object? Are you using the { useMasterKey: true } option in find for the one and not the other?

If that’s all fine, I’m not sure how else I can help…unless you have a sharable repo I can take a look at.