简体   繁体   English

解析云代码。 无法定义关系

[英]Parse Cloud Code. Unable to define relation

I am calling this function from my android app, 我正在从我的android应用程序调用此函数,

Parse.Cloud.define('addFriendRequest', function(request, response) {
var userObjectId = request.params.userObjectId;


var User = Parse.Object.extend('_User'),
    user = new User({ objectId: userObjectId });

var relation = Parse.Relation(user, 'friendRequests');
relation.add(request.user);

Parse.Cloud.useMasterKey();
user.save().then(function(user) {
    response.success("Successfully added friend Request");
}, function(error) {
    response.error("An error has occurred")
});

}); });

And an errror is being thrown of type 并且错误类型被抛出

TypeError: Cannot call method 'add' of undefined at main.js:10:11 TypeError:无法在main.js:10:11调用未定义的方法“ add”

I am relatively new to javascript so any advice would be great. 我对javascript比较陌生,所以任何建议都很棒。 Also the relation friendRequests exists already. 关系friendRequests已经存在。

This should work. 这应该工作。

Parse.Cloud.define('addFriendRequest', function(request, response) {
Parse.Cloud.useMasterKey();
var userObjectId = request.params.userObjectId;
var user = Parse.User.createWithoutData(userObjectId);
var relation = user.relation('friendRelation');
relation.add(request.user);
user.save().then(function(user) {
    response.success("Successfully added friend Request");
}, function(error) {
    response.error("An error has occurred")
});
});

Some clarification of what is going on here and why your code did not work. 一些澄清这里发生了什么,以及为什么您的代码无法正常工作。

  1. First of all, it is a good practice to put Parse.Cloud.useMasterKey(); 首先,放置Parse.Cloud.useMasterKey();是一个好习惯Parse.Cloud.useMasterKey(); right in the beginning of the function. 就在函数的开头
  2. I see that you have the objectId of user in the beginning and that you would like to use the user based on its objectId. 我看到您一开始就有user的objectId,并且您想根据其objectId使用该用户。 I order to do that, you should use Parse.User.createWithoutData('yourObjectIdHere') method. 我要这样做,您应该使用Parse.User.createWithoutData('yourObjectIdHere')方法。 What you are actually doing for this step, is that you are creating a new user, rather then creating a reference for the one you already have in the database. 此步骤的实际操作是创建一个新用户,而不是为数据库中已有的用户创建引用。
  3. If you want to get a relation of a particular object (User object in your case) your use Parse.Object.relation('putRelationKeyHere') . 如果要获取特定对象(在您的情况下为User对象)的关系,请使用Parse.Object.relation('putRelationKeyHere') You are actually creating a new relation instead of accessing the existing one. 您实际上是在创建一个新的关系,而不是访问现有的关系。

I would recommend you to read Parse Javascript Guide in order to learn the recommended techniques. 我建议您阅读Parse Javascript Guide ,以了解推荐的技术。

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

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