简体   繁体   English

如何将JavaScript变量中保存的_User对象转换回其格式,可以将其保存到parse.com指针列中

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

Using parse.com and the JavaScipt SDK. 使用parse.com和JavaScipt SDK。

I've got a variable (friendName) that stores a _User record in it. 我有一个变量(friendName)在其中存储_User记录。

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. 这样就可以将名称重新关联到_User类。

When I try and save the user using 当我尝试保存用户使用

  requestFriend.set("username", friendName);

I get the following error in Chrome dev tools. 我在Chrome开发工具中收到以下错误。

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

To me this indicates that "friendName" is not in the correct format to save back to parse. 对我来说,这表明“ friendName”的格式不正确,无法保存回解析。

But have do I convert it into a _User object to that it can save successfully? 但是我是否将其转换为可以成功保存的_User对象?

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. 附有屏幕截图。

在此处输入图片说明

Sounds like your schema is expecting it to be a User object. 听起来您的架构期望它是一个User对象。 If friendName has the objectId of the user, you can create the object and the SDK will convert it to a pointer 如果friendName具有用户的objectId,则可以创建对象,然后SDK会将其转换为指针

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

If friendName is the username, you'd have to first query for the User and then set it. 如果friendName是用户名,则必须首先查询用户,然后进行设置。 Something like this: 像这样:

new Parse.Query("_User").equalTo("username", friendName).first().then(function(user) {
    requestFriend.set("username", user);
    return requestFriend.save();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM