我正在尝试在解析时运行一些云函数,但originalFavorited.first
没有运行。
var _ = require('underscore');
Parse.Cloud.define("userFeed", function (request, response) {
if (request.user == undefined) {
response.error("User must be logged in!");
}
var friendsQuery = new Parse.Query("Friend");
friendsQuery.equalTo("fromUser", request.user);
var badPhotosQuery = new Parse.Query("Photo");
badPhotosQuery.greaterThanOrEqualTo("flagCount", 3);
var userFlaggedQuery = new Parse.Query("Photo");
userFlaggedQuery.containedIn("flagCount", request.user.flaggedItems);
var allBadPhotosQuery = new Parse.Query.or(badPhotosQuery, userFlaggedQuery);
var activitiesQuery = new Parse.Query("Activity");
activitiesQuery.skip(request.params.startRecord);
activitiesQuery.limit(request.params.numberRecords);
activitiesQuery.equalTo("type", "favorite");
activitiesQuery.matchesKeyInQuery("fromUser", "toUser", friendsQuery);
activitiesQuery.descending("createdAt");
activitiesQuery.include("photo")
activitiesQuery.include("fromUser");
activitiesQuery.notEqualTo("fromUser", request.user);
activitiesQuery.doesNotMatchQuery("photo", allBadPhotosQuery);
var parsedResults = [];
activitiesQuery.find().then(function (results) {
return results;
}).then(function (results) {
var promise = Parse.Promise.as();
_.each(results, function (activity) {
promise = promise.then(function () {
console.log("Parsing results for " + activity);
var originalFavorited = new Parse.Query("Activity");
originalFavorited.ascending("createdAt");
console.log(activity.get("photo"));
originalFavorited.equalTo("photo", activity.get("photo"));
originalFavorited.include("photo");
originalFavorited.include("fromUser");
originalFavorited.equalTo("type", "favorite");
originalFavorited.first(function (origResult) {
console.log("Found original");
parsedResults.push(origResult);
return parsedResults;
});
});
});
return promise;
}).then(function () {
var filteredResults = _.uniq(parsedResults, false);
response.success(filteredResults);
});
我的输出console.log (activity.get("photo"));
正在输出对象。 photo
是指向Photo
班级的指针。
我是否正确地执行了承诺,以便同步运行代码?
答案 0 :(得分:0)
试试这个:
var parsedResults = [];
activitiesQuery.find().then(function (results) {
/// ---- REMOVED HERE ----
// return results;
// }).then(function (results) {
var promise = Parse.Promise.as();
_.each(results, function (activity) {
promise = promise.then(function () {
console.log("Parsing results for " + activity);
var originalFavorited = new Parse.Query("Activity");
originalFavorited.ascending("createdAt");
console.log(activity.get("photo"));
originalFavorited.equalTo("photo", activity.get("photo"));
// the two line belows can be ignored since you have the same value in activity.get("photo") and activity.get("fromUser"), included previously
originalFavorited.include("photo");
originalFavorited.include("fromUser");
originalFavorited.equalTo("type", "favorite");
/// ---- ADDED LINE BELOW ----
originalFavorited.limit(1);
originalFavorited.find(function (subResults) {
console.log("Found original");
/// ADDED CHECK HERE
if (subResults.length > 0){
parsedResults.push(subResults[0]);
}
/// ---- CHANGED HERE ----
var subPromise = Parse.Promise.as();
subPromise.resolve(parsedResults);
return subPromise ; // returning a promise to promise series
/// ---- ADDED ERROR HANDLING HERE ----
},function(error){
console.log("Unable to retrieve first element in originalFavorited query. Cause: " + error.message);
var errPromise = Parse.Promise.as();
errPromise.reject(error);
return errPromise; // return failed promise to promise series just for continue the operation series up to the end
});
});
});
return promise;
},function(error){ /// ---- ADDED ERROR HANDLING HERE ----
console.log("Unable to perform find on activitiesQuery : " + error.message);
response.error(error.message);
}).then(function () {
var filteredResults = _.uniq(parsedResults, false);
response.success(filteredResults);
});
注意:不要忘记自定义函数的解析限制执行,因此请正确选择要为每个自定义函数调用执行的查询量