我在User类中编写一个友谊关系,该类针对一个补充类(名为Matrix)。目标是为每个朋友提供其他信息。
案例1:iOS / Swift提供了预期的结果。
func saveRelation(user: User, state: Bool) {
if let dbOwner = self.dbUser, dbPeer = user.dbUser {
let dbMatrix = PFObject(className: "Matrix")
dbMatrix["userid"] = dbPeer
dbMatrix.saveInBackgroundWithBlock( { (success: Bool, error: NSError?) -> Void in
if(error == nil) && (success == true) {
let relation = dbOwner.relationForKey("friend")
relation.addObject(dbMatrix)
dbOwner.saveInBackgroundWithBlock( { (success: Bool, error: NSError?) -> Void in
if(error == nil) && (success == true) {
self.directory.friend.append(user)
Trace.log("Relation \(user.account.id)", message: "friend")
} else {
ParseError.handle(error!)
}
} )
}
} )
}
}
其中dbOwner是currentUser,而dbPeer是从列表中拾取的
情况2:解析具有意外关系的Cloud代码结果。仅插入一个关系。
var UserGet = function(id, res) {
var query = new Parse.Query(Parse.User);
query.get(id, {
success: function(dbUser) { res.success(dbUser); },
error: function(error) { res.error(error); }
} );
}
Parse.Cloud.define('Relation.friend', function(req, res) {
Parse.Cloud.useMasterKey();
var owner = req.user;
var query = UserGet(req.params.peerId, {
success: function(dbPeer) {
var Matrix = Parse.Object.extend("Matrix");
var matrix = new Matrix();
matrix.set("userId", dbPeer);
matrix.save(null, {
success: function(dbMatrix) {
var relation = owner.relation("friend");
relation.add(dbMatrix);
owner.save(null, {
success: res.success(true),
error: res.error("Update.failed") } );
},
error: function(_error) {
res.error("Save.failed");
}
});
},
error: function(_error) {
res.error("Query.failed");
}
} );
} );
客户呼叫如下:
func saveRelation(user: User, state: Bool) {
PFCloud.callFunctionInBackground("Relation.friend",
withParameters: [ "peerId" : user.account.id
])
{ (success: AnyObject?, error: NSError?) -> Void in
if(error == nil) && (success as? Bool == true) {
self.directory.friend.append(user)
Trace.log("Relation \(user.account.id)", message: "friend")
}
}
}
任何帮助表示赞赏。
最佳答案
我已经解决了我的问题,而没有为每个请求设置“ req.user”。
查看服务器日志显示,只有第一个请求会添加关系,而第二个调用会携带带有空值的“ req.user”。
'content-length': '326',
connection: 'close' } {
"friend": {
"__op": "AddRelation",
"objects": [
{
"__type": "Pointer",
"className": "_User",
"objectId": "kdja9IlKgm"
}
]
}
}
'content-length': '217',
connection: 'close' } {}
verbose: {
"response": {
"updatedAt": "2016-04-28T05:25:00.282Z"
}
}
因此,我使用2个参数“ fromId”和“ toId”更改了函数API,可在任何调用时查询。
Parse.Cloud.define('Relation.friend', function(req, res) {
Parse.Cloud.useMasterKey();
var queryTo = UserGet(req.params.toId, {
success: function(dbTo) {
var queryFrom = UserGet(req.params.fromId, {
success: function(dbFrom) {
var Matrix = Parse.Object.extend("Matrix");
var matrix = new Matrix();
matrix.set("userId", dbTo);
matrix.save(null, {
success: function(dbMatrix) {
var relation = dbFrom.relation("friend");
relation.add(dbMatrix);
dbFrom.save(null, {
success: function() {
console.log("<<<Update.Success>>>");
res.success(true);
},
error: function(_error) {
console.log("<<<Update.failed>>>");
res.error("Update.failed");
}
} );
},
error: function(_error) {
console.log("<<<Save.failed>>>");
res.error("Save.failed");
}
} );
},
error: function(_error) {
res.error("<<<QueryFrom.failed>>>");
}
} );
},
error: function(_error) {
res.error("<<<QueryTo.failed>>>");
}
} );
} );
客户端函数调用将相应更改。
func saveRelation(user: User, state: Bool) {
PFCloud.callFunctionInBackground("Relation.friend",
withParameters: [
"fromId": owner.account.id,
"toId" : user.account.id
])
{ (success: AnyObject?, error: NSError?) -> Void in
if(error == nil) && (success as? Bool == true) {
self.directory.friend.append(user)
Trace.log("Relation \(owner.account.id) & \(user.account.id)", message: "friend")
} else {
ParseError.handle(error!)
}
}
}
关于javascript - 解析:Cloud JS代码与Swift不会产生相同的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36897800/