简体   繁体   English

使用q.js承诺时出现问题

[英]Issue while using q.js promises

The following code used q.js to fetch details of students. 以下代码使用q.js来获取学生的详细信息。 Everything works properly except that the fetched categories and city are not mapped in variable studentdetails . 一切工作正常,除了未将获取的类别和城市映射到可变的studentdetails It only contains the details(name,email,id,dob) fetched by the mail query. 它仅包含由邮件查询获取的详细信息(名称,电子邮件,id,dob)。 Where am I going wrong? 我要去哪里错了? Any leads will be highly appreciated. 任何线索将不胜感激。

//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

You may try this : 您可以尝试以下方法:

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
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM