gpt4 book ai didi

javascript - 第一个查询完成后如何在 parse.com 上调用第二个查询(云代码)

转载 作者:行者123 更新时间:2023-11-30 17:47:39 25 4
gpt4 key购买 nike

我的挑战是我需要先执行一个查询,然后使用第一个查询的结果作为第二个查询的输入。

var adList = [];
query.find({
success: function(results) {
for (var i=0; i<results.length; i++){
var ad = [];
ad.push(results[i].get("Type")); //Adds "Type" to the ad array
objectIDArray.push(results[i].id);
}
},
error: function(){
response.error("failed");
}
});
//second query
var locations = Parse.Object.extend("Locations");
query2.include("locationID");
query2.containedIn("campaignIDString", objectIDArray);
query2.find({
success: function(results){
locations = results2[0].get("locationID");
adList.push(locations.get("CITY"));
adList.push(locations.get("PROVINCE"));
adList.push(locations.get("STORE_ADDRESS"));

response.success(adList);
}, error: function(){
response.error("failed to get a response");
}
});

如您所见,在第二个查询中我需要第一个查询填充的 objectIDArray。如果我运行它,我总是在第二个查询中得到空结果,因为这两个查询似乎是并行发生的。无论如何,它们不会像我希望的那样依次发生。如何让我的第二个查询在第一个查询之后运行?使用 promise ?

你能举个例子吗,我从文档中看不太懂

最佳答案

只需将第二个查询移动到第一个查询的完成 block 中:

var adList = [];
query.find({
success: function(results) {
for (var i=0; i<results.length; i++){
var ad = [];
ad.push(results[i].get("Type")); //Adds "Type" to the ad array
objectIDArray.push(results[i].id);
}

//second query
var locations = Parse.Object.extend("Locations");
query2.include("locationID");
query2.containedIn("campaignIDString", objectIDArray);
query2.find({
success: function(results){
locations = results2[0].get("locationID");
adList.push(locations.get("CITY"));
adList.push(locations.get("PROVINCE"));
adList.push(locations.get("STORE_ADDRESS"));

response.success(adList);
}, error: function(){
response.error("failed to get a response");
}
});
},
error: function(){
response.error("failed");
}
});

或者您可以使用 Promises .

关于javascript - 第一个查询完成后如何在 parse.com 上调用第二个查询(云代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19794012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com