我正在尝试在parse.com上编写以下云代码。我有三个类User,Brand和Campaign。我将LastLoginTime保留在User类中。品牌类具有名为Campaignigns to Campaign类的关系。我需要从用户检索LastLoginTime然后获取updateAt日期大于LastLoginTime的所有广告系列,最后我需要检索与这些广告系列相关的品牌。到目前为止,我只能创建一个函数来获取LastLoginTime。我曾尝试使用matchesKeyInQuery或链式函数,但还没有能够解决。这是迄今为止的代码。
Parse.Cloud.define("getLoginTime",function(request, response){
var User = Parse.Object.extend("User");
var query = new Parse.Query(User);
var lastlogin;
query.equalTo("objectId", request.params.objectId);
query.first({
success: function(object) {
// The object was retrieved successfully.
lastlogin = object.get("LastLoginTime");
response.success(lastlogin); // works well if left alone :)
console.log("entered time");
console.log(lastlogin);
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and description.
}
});
});
以上作品,下面没有:
Parse.Cloud.define("getNewCampaigns", function (request, response){
var Campaign = Parse.Object.extend("Campaigns");
var cquery = new Parse.Query(Campaign);
var lastlogin;
lastlogin = parse.Cloud.run("getloginTime",request.params.objectId);
cquery.greaterThan("updatedAt",lastlogin);
cquery.find({
success: function(results) {
// results is an array of Parse.Object.
response.success(results);
},
error: function(error) {
// error is an instance of Parse.Error.
}
});
});
有没有办法或者我需要改变我的模型。
答案 0 :(得分:1)
Parse.Cloud.run
会返回Promise(More on Promises for Parse here),您也可以将回调作为第三个参数发送。
使用回调代码应该看起来像这样:
Parse.Cloud.run("getloginTime", request.params.objectId, {
success: function(lastlogin) {
cquery.greaterThan("updatedAt",lastlogin);
cquery.find({
success: function(results) {
// results is an array of Parse.Object.
response.success(results);
},
error: function(error) {
// error is an instance of Parse.Error.
}
});
},
error: function(error) {}
});
在这里看到令人讨厌的筑巢? 使用promises,您的代码将看起来像这样:
Parse.Cloud.run("getloginTime", request.params.objectId).then(function(lastlogin) {
var cquery = new Parse.Query(Campaign);
cquery.greaterThan("updatedAt", lastlogin);
return cquery.find();
}).then(function(results) {
// results is an array of Parse.Object.
response.success(results);
}, function(error) {
// error is an instance of Parse.Error.
});