问题
I was using an old parse SDK version 1.5.0 and my function was returning with all the includes. Now I tried to use the latest SDK and the function is returning the main object only (on the gate and location I get "pointers" only.).
Here is the code:
Parse.Cloud.define("get_gates_for_user", function(request, response) {
var userId = request.params.userId;
var gateToUserQuery = new Parse.Query("GateUserJoinTable");
gateToUserQuery.equalTo("user", {
__type: "Pointer",
className: "_User",
objectId: userId
});
gateToUserQuery.include("gate");
gateToUserQuery.include("location");
gateToUserQuery.find({
success: function(results) {
response.success(results);
},
error: function(error) {
console.log(error.message);
response.error(ERROR_CODE_GENERAL_ERROR);
}
});
});
回答1:
I started working with Parse recently so I'm not very familiar with the behavioiur of old SDK versions.
Since you're in Cloud Code, however, .include()
doesn't warrant a significant performance gain over .fetch()
, since the code runs on their infrastructure (it's the documented way of accessing related Parse.Object
s, so they should be optimizing for that anyway), so the following should work:
var _ = require('underscore');
var results;
gateToUserQuery.find().then(function (joins) {
// add results to bigger-scoped variable
// for access in the other function
results = joins;
// Promises are beautiful
var fetchPromises = _.map(results, function (join) {
return Parse.Promise.when([
join.get('gate').fetch(),
join.get('location').fetch()
]));
});
return Parse.Promise.when(fetchPromises);
}).then(function () {
// related Pointers should be filled with data
response.success(results);
});
I think at least in the current iteration of the SDK, include only works with Arrays instead of Pointers.
来源:https://stackoverflow.com/questions/33064173/parse-cloud-code-new-sdk-include-subclass-not-working