mysql - 执行函数前的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 用于下一个查询并将该结果附加为响应,但在我的导出模块执行之前执行响应
解决方案
//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;
推荐阅读
- android - 即使在 React Native 中设置了 android:usesCleartextTraffic="true" 和网络安全配置,也无法使用 axios 发送 HTTP
- angular - 在 httpclient.put 上,日期正在从 UTC 转换为 BST
- python-3.x - 如何从变量定义 DotMap KeySet Key
- postman - Postman & Newman - cookieJar.getAll() 需要回调函数
- swift - SwiftUI,如何阻止 GridItem 扩展
- angular - 使用 angular-testing-library 测试通过“类构造函数”更新的值
- webpack - 在 webpack 配置中为 node-sass 设置包含路径
- angular - 当我在 Angular 项目中使用 lite-server 时,为什么会得到 404 GET /index.html?
- javascript - 使用 ajax 和 asp.net 将 html5 音频内容上传到服务器
- azure-devops - Azure DevOps Git Repo SSH 无法下载 lfs 文件,而 https 工作正常