[英]Parse : Cloud JS code vs Swift does not produce the same results
I'm coding a friendship relation in the User class which targets a supplementary class (named Matrix). 我正在针对一个补充类(名为Matrix)的User类中编写一个友谊关系。 The goal is to provide additional info for each friend.
目标是为每个朋友提供其他信息。
Case #1 : iOS/Swift provides the expected results. 案例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!)
}
} )
}
} )
}
}
where dbOwner is the currentUser and dbPeer is pickup from a list 其中dbOwner是currentUser,而dbPeer是从列表中拾取的
Case #2 : Parse Cloud code results with unexpected relations. 情况2:解析具有意外关系的Cloud代码结果。 Only one relation being inserted.
仅插入一个关系。
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");
}
} );
} );
with the client call as bellow : 客户呼叫如下:
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")
}
}
}
Any help appreciated. 任何帮助表示赞赏。
I've solved my issue without thrusting 'req.user' is set for each request. 我已经解决了我的问题,而没有为每个请求设置“ req.user”。
Looking at server logs shows that only the first request adds a relation, while a second call carry a 'req.user' with a null value. 查看服务器日志显示,只有第一个请求会添加关系,而第二个调用会携带带有空值的“ 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"
}
}
Thus, I've changed my function API with 2 parameters, 'fromId' and 'toId', which are queried at any call. 因此,我已使用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>>>");
}
} );
} );
Client function call is changed accordingly. 客户端函数调用将相应更改。
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!)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.