首页 > 解决方案 > 执行函数前的api代码响应(回调)

问题描述

我已经用 node.js 实现了 restful api,但是在 api 响应之后我从回调方法得到响应我必须使用 song_id 来获取详细信息歌手。

下面是我的 song.js 文件

var express = require('express');
var router = express.Router();
var moviecontents = require("./movies_content.js");
router.get('/:id', function(req, res) {
    res.locals.connection.query('select song_id, song_title,     song_duration from view_song_movie_mapping where movie_id=? ORDER BY song_id ASC',[req.params.id], function (error, results, fields) {
    if(error){
        res.send(JSON.stringify({"status": 500, "error": error, "response": null})); 
    } else {

        var aResult = [];
        var aResultNew = [];

           for (var i in results){
            var myreq = new Object();
            myreq.song_id = results[i].song_id;
            myreq.role_id = 1556;
            aResult.push(results[i]);
            moviecontents.getSongPersonView(myreq, res, function(err, dataval){
                       if(err) throw err;
                        aResultNew.push(dataval);

            });


          }
        res.send(JSON.stringify({"status": 200, "error": null, "response": aResult}));
    }
});
});

module.exports = router;

我在 song_details.js 下面调用导出模块

var express = require('express');
var exports = module.exports = {};
module.exports.getSongPersonView = function (req,res,callback){
    res.locals.connection.query('SELECT person_id, person_name FROM view_song_person_mapping WHERE song_id = ? and role_id = ? order by song_person_id DESC',[req.song_id,req.role_id], function (error, results, fields) {
        if(error){
            return res.send(JSON.stringify({"status": 500, "error": error, "response": null})); 
            //If there is error, we send the error in the error section with 500 status
        } else {
            callback(null, results);
        }
    });
};

我想使用第一个查询中的 song_id 将该 song_id 用于下一个查询并将该结果附加为响应,但在我的导出模块执行之前执行响应

标签: mysqlnode.js

解决方案


//Here is the answer
var express = require('express');
var router = express.Router();

var async = require("async");
function getSongSingerDetails (song_id,res) {
  return new Promise(function(resolve, reject){
    res.locals.connection.query('SELECT person_id, person_name FROM view_song_person_mapping WHERE song_id = ? and role_id = ? order by song_person_id DESC',[song_id,1556], function (error, results, fields) {
        if(error){
            res.send(JSON.stringify({"status": 500, "error": error, "response": null})); 
        } else {
            resolve(results);
        }
    });
});
}

function getSongListing (req, res) {
    return new Promise(function(resolve, reject){
    res.locals.connection.query('select song_id, song_title, song_duration from view_song_movie_mapping where movie_id=? ORDER BY song_id ASC',[req.params.id], function (error, results, fields) {
        if(error){
            res.send(JSON.stringify({"status": 500, "error": error, "response": null})); 
        } else {
            resolve(results);
        }
    });
});
    }
async function getsongdetails(req, res){
   var songlist = await getSongListing(req, res);
  for (var i=0; i<songlist.length; i++) {
     var singerdetails = await getSongSingerDetails(songlist[i].song_id,res);
     songlist[i]['singers'] = [];
     songlist[i]['singers'] = singerdetails;
  }
  res.send({"status": 200, "error": null, "response": songlist});
  }

   router.get('/:id', function(req, res) {
 var results = getsongdetails(req, res);
   });
module.exports = router;

推荐阅读