gpt4 book ai didi

javascript - 使用 q.js Promise 时出现问题

转载 作者:行者123 更新时间:2023-11-29 11:20:00 25 4
gpt4 key购买 nike

以下代码使用 q.js 获取学生的详细信息。除了获取的类别和城市未映射到变量 studentdetails 中之外,一切正常。它仅包含邮件查询获取的详细信息(姓名、电子邮件、ID、出生日期)。我哪里错了?任何线索都将受到高度赞赏。

//fetches category of student
var getJobCategories = function(student) {
var deferred = Q.defer();
var categoryquery = 'select c.name,c.id from category c,student_category sc where xxxx';
db.query(categoryquery, function(err, categories, fields) {
if(categories.length == 0) {
student.categories = "NA";
} else {
student.categories = categories;
}
console.log(student)//prints details with categories
deferred.resolve();
});
return deferred.promise;

}
//fetches city of student
var getCurrentCity = function(student) {
var deferred = Q.defer();
var cityquery = 'select l.name,l.id from location l ,student_location sl where xxxx';
db.query(cityquery, function(err, city) {
if(city.length == 0) {
student.currentcity = "NA";
} else {
student.currentcity = city;
}
console.log(student)//prints details with city
deferred.resolve();
});
return deferred.promise;

}

var query='select name,email,id,dob from student limit 1,10';
db.query(query, function(err1, studentdetails) {
var promise1=studentdetails.map(function(student){
var result=getJobCategories(student)

});
var promise2=studentdetails.map(function(student){
getCurrentCity(student)
});

var allpromises= Q.all([
promise1,promise2

]);

Q.allSettled(allpromises)
.then(function (results) {
next(null,{
result: studentdetails,
msg: "Fetched successfully"
});
});//then
});//query

最佳答案

你可以尝试这个:

db.query(query, function(err1, studentdetails) {
var allStudentJobCategories =
studentdetails.map(function(student){
return getJobCategories(student)
});

var allStudentCities =
studentdetails.map(function(student){
return getCurrentCity(student)
});

var allpromises= Q.all([
Q.all(allStudentJobCategories), Q.all(allStudentCities)
]);

Q.allSettled(allpromises)
.then(function (results) {
next(null,{
result: studentdetails,
msg: "Fetched successfully"
});
});//then
});

关于javascript - 使用 q.js Promise 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39076812/

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