在我的应用中,User
可以请求加入另一个Users
帐户。伪代码如下:
username
username
no
返回yes
创建新的AccountRequest
对象AccountRequest
对象添加到我们正在搜索的用户。我能够完成步骤1-4但是我在完成#5时遇到了麻烦。
这是我正在使用的代码。
Parse.Cloud.define("sendAccountAdditionRequest", function(request, response) {
console.log("-sendAccountAdditionRequest");
// Create the query on the User class
var query = new Parse.Query(Parse.User);
// Set our parameters to search on
query.equalTo("username", request.params.adminUsername);
// Perform search
query.find({
// We found a matching user
success: function(results) {
Parse.Cloud.useMasterKey();
var fetchedUser = results[0]
console.log("--found user");
console.log("--creating new AccountRequest");
// Create a new instance of AccountRequest
var AccountRequestClass = Parse.Object.extend("AccountRequest");
var accountRequest = new AccountRequestClass();
// Set the User it is related to. Our User class has a 1..n relationship to AccountRequest
accountRequest.set("user", fetchedUser);
// Set out other values for the AccountRequest
accountRequest.set("phone", request.params.adminUsername);
accountRequest.set("dateSent", Date.now());
// Save the new AccountRequest
accountRequest.save(null,{
// Once the new AccountRequest has been saved we need to add it to our fetched User
success:function(savedRequest) {
console.log("---adding AccountRequest to fetched User");
//
// === This is where stuff breaks
//
var requestRelation = fetchedUser.relation("accountRequest");
// Now we need to add the new AccountRequest to the fetched User. The accountRequest property for a User is array...I'm not sure how I'm suppose to append a new item to that. I think I need to somehow cast results[0] to a User object? Maybe?
requestRelation.add(savedRequest);
// We perform a save on the User now that the accountRequest has been added.
fetchedUser.save(null, {
success:function(response) {
console.log("----AccountRequest complete");
response.success("A request has been sent!");
},
error:function(error) {
// This is printing out: ParseUser { _objCount: 2, className: '_User', id: 'QjhQxWCFWs' }
console.log(error);
response.error(error);
}
});
//
// ================================
//
//response.success("A request has been sent!");
},
// There was an error saving the new AccountRequest
error:function(error) {
response.error(error);
}
});
},
// We were not able to find an account with the supplied username
error: function() {
response.error("An account with that number does not exist. Please tell your administrator to sign up before you are added.");
}
});
});
我相信我的问题是从初始查询返回的搜索结果中获取accountRequest
关系。另外需要注意的是,我没有创建accountRequest
的{{1}}属性,我的理解是,当我执行PFUser
函数时,这将由Parse自动完成。
答案 0 :(得分:1)
是否将为您创建accountRequest取决于是否将类级别权限设置为允许客户端应用程序添加字段。对于你的Parse.User,这可能设置为NO。
但是无论如何都不需要Parse.User上的关系,因为你已经在AccountRequest类上建立了它。给定用户,您可以通过以下方式获取帐户请求:
PFQuery *query = [PFQuery queryWithClassName:@"AccountRequest"];
[query whereKey:@"user" equalTo:aUser];
query.find()...
这相当于在User上获取关系,获取查询并运行它。
关于您的代码的一些注意事项:(a)findOne
会在您知道只有一个结果时为您节省一条线,(b)使用Parse.Promise
会非常整理。