How to convert a _User object held in JavaScript variable back to format it can be saved into a parse.com pointer column

StackOverflow https://stackoverflow.com/questions/23687922

Question

Using parse.com and the JavaScipt SDK.

I've got a variable (friendName) that stores a _User record in it.

I know want to save this user back into parse under a new class and into a "pointer" column type. This is so the name can then be associated back to the _User class.

When I try and save the user using

  requestFriend.set("username", friendName);

I get the following error in Chrome dev tools.

{"code":111,"error":"invalidtypeforkeyusername,expected*_User,butgotstring"}

To me this indicates that "friendName" is not in the correct format to save back to parse.

But have do I convert it into a _User object to that it can save successfully?

I can post up all code if it helps, but I thought I'd try and keep the post shorter to start with.

Screen shots attached.

enter image description here

Was it helpful?

Solution

Sounds like your schema is expecting it to be a User object. If friendName has the objectId of the user, you can create the object and the SDK will convert it to a pointer

new Parse.User({objectId: friendName});

If friendName is the username, you'd have to first query for the User and then set it. Something like this:

new Parse.Query("_User").equalTo("username", friendName).first().then(function(user) {
    requestFriend.set("username", user);
    return requestFriend.save();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top