这部分是一个parse.com问题,他们的API是独一无二的。但是,我遇到的问题有一点,因为我是javascript的新手,并没有完全不感兴趣。
我的代码很好地评论了我的想法!有一段代码在所有大写中都有注释,这是我遇到问题的部分。
如何在我的代码中控制javascripts异步调用的任何见解将非常有用。
CODE
Parse.Cloud.define("StartSession", function(request, response) {
var query =new Parse.Query("User"); // search the User class
query.containedIn("username", request.params.myArray); //get the users that are in my array
query.find({
success: function(results) { // if found do this
// Didn't find a keyword to match tags so create new keyword
//result is all the users that are in this session to be created.
var Session = Parse.Object.extend("Session"); // create a session objsect
var mySession = new Session(); // create an instance of the session
console.log("the log is");
var users = []; // all the users
var deviceID = []; // their corsponding ids
users.push(request.params.user); // add an extra item to the users array that is being passed into this funciton on the original call
for (var i = 0; i <= results.length - 1; i++) { // iterate all of the results returned from the find
users.push(results[i].get("username")); // get their username attribute and add it to the users array
deviceID.push(results[i].get("installID")); // get their associated installID and add it to deviceID
};
// users.splice(users.indexOf(request.params.user), 1);
mySession.set("users", users); // put all of the supplied users found into this session
mySession.set("owner", request.params.user) // set the owner of the session. This was passed in
console.log(results);
var installs = new Parse.Query(Parse.Installation); // get the installs class
mySession.save(null, { // save the session
success: function(gameScore) { //if successful go on
// Execute any logic that should take place after the object is saved.
console.log("getting"); // logging
for (var i = deviceID.length - 1; i >= 0; i--) { //iterate all of the deviceIDs from the above
var sessionID = mySession.id; // get the session id for the session that was just created
installs.contains("objectid", deviceID[i]); // make a search for the installs class
installs.find({ // find given the above search
success: function(myResults){ // if success
console.log("getting2"); // log something
var tempChannels = myResults.get('channels'); // get the given users channels list and save it into an array
console.log(myResults); // log something
tempChannels.push(sessionID); // add the new sessionId to their list of channels
myResults.set("channels", tempChannels); // put back the newly modified array which should be a list of channels
}, // THE SUCCESS CODE ABOVE IS NOT WORKING OR DOING ANYTHING. CHANNEL IS NOT CHANGING NOR IS ANYTHING GETTING LOGGED
error: function(error){ // NOR IS THE ERROR CODE
console.log("getting 3")
console.log("asD");
}
});
// var channels = deviceID[i].get("channels");
// console.log("adding a channel for "+deviceID[i]);
// channels.push(sessionID);
// deviceID[i].set("channels", channels);
}; //THE REST IS UNIMPORTANT FOR THIS QUESTI
var targets = new Parse.Query(Parse.Installation);
console.log("check point 1 "+sessionID);
//targets.equalTo('deviceType', ['android']);
targets.contains("channels", sessionID)
if (targets.length > 0) {console.log("There are more than one recipiants for this")};
console.log("check point 2");
console.log(targets.length);
Parse.Push.send({
where: targets,
data: {
alert: "test",
}
}, {
success: function() {
// Do something on success
console.log("Was able to send notifications");
console.log("check point 3");
},
error: function() {
// Do something on error
console.log("Error sending the notifications");
console.log("check point 4");
}
});
response.success(users);
},
error: function(gameScore, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and description.
response.success("ERROR");
console.log("check point 5");
}
});
},
error: function() {
response.error("Found nothing");
console.log("check point 6");
}
});
});
我发现了一些关于同一类here的帖子,但我没有错误地安装一个查询。我在循环中为每次迭代创建一个
主要问题是:
for (var i = deviceID.length - 1; i >= 0; i--) { //iterate all of the deviceIDs from the above
var sessionID = mySession.id; // get the session id for the session that was just created
installs.contains("objectid", deviceID[i]); // make a search for the installs class
installs.find({ // find given the above search
success: function(myResults){ // if success
console.log("getting2"); // log something
var tempChannels = myResults.get('channels'); // get the given users channels list and save it into an array
console.log(myResults); // log something
tempChannels.push(sessionID); // add the new sessionId to their list of channels
myResults.set("channels", tempChannels); // put back the newly modified array which should be a list of channels
}, // THE SUCCESS CODE ABOVE IS NOT WORKING OR DOING ANYTHING. CHANNEL IS NOT CHANGING NOR IS ANYTHING GETTING LOGGED
error: function(error){ // NOR IS THE ERROR CODE
console.log("getting 3")
console.log("asD");
}
没有执行。没有错误日志没有成功日志只是没有被调用。
我觉得这与异步性质有关。进程终止,调用永远不会被回调。
答案 0 :(得分:5)
我认为你是对的,问题的根源似乎是由于回调。在突出显示的for循环中,您不会在继续之前等待那些find()调用完成。推特之后也是如此。我已经大大简化了您的代码,以说明正在发生的事情。简而言之,在asyncs完成之前调用response.success(),因此您永远不会看到回调。
Parse.Cloud.define("StartSession", function(request, response) {
query.find({
success: function(results) {
mySession.save(null, {
success: function(gameScore) {
for (var i = deviceID.length - 1; i >= 0; i--) {
installs.find(); // not waiting for this
};
Parse.Push.send(); // not waiting for this
// called before the async's fire so no callbacks happen
response.success(users);
},
error: function(gameScore, error) {}
});
},
error: function() {}
});
});
我建议使用Promises来简化一些事情。这看起来像这样:
Parse.Cloud.define("StartSession", function(request, response) {
query.find().then(function(results) {
return mySession.save();
}).then(function(mySession) {
var installQueries = [];
for (var i = deviceID.length - 1; i >= 0; i--) {
installQueries.push(installs.find()); // not waiting for this
};
// wait for all the install queries to finish
return Parse.Promise.when(installQueries);
}).then(function() {
return Parse.Push.send();
}).then(response.success, response.error);
});