获取nodejs中另一个控制器中的一个控制器的响应
我面临一个问题,以获得来自另一个控制器内的一个控制器的响应。
如何从getUserAppointmentDetails.js获取responseAppointment.js文件的响应
getUserAppointmentDetails.js const con = require('./../database_connection'); exports.getUserAppointmentDetails = function (req, res, next) { // const details = req.body; con.query('SELECT * from user_appointment_details WHERE id = 1', (err, respond) => { if (err) throw err; res.send({result: respond}); }); } Another file respondAppointment.js const con = require('./../database_connection'); const userDetails = require('./getUserAppointmentDetails'); exports.respondAppointment = function (req, res, next) { const details = req.body; con.query('update user_appointment_details SET status = "' + details.status + '" WHERE id = ' + details.id, (err, respond) => { if (err) throw err; res.send({result: respond}); }); var userData = userDetails.getUserAppointmentDetails(req, res, next); }
如果有人有任何想法请回应。
这里的问题是,你在发送响应,而不是实际返回从另一个函数callback的结果。
假设您想要getUserAppointmentDetails.js
中的respondAppointment.js
结果。 在getUserAppointmentDetails.js
和getUserAppointmentDetails.js
进行更改以将该值返回给调用函数。
getUserAppointmentDetails.js
const con = require('./../database_connection'); exports.getUserAppointmentDetails = function (idToGet) { // const details = req.body; con.query('SELECT * from user_appointment_details WHERE id = '+idToGet, (err, respond) => { if (err) throw err; return respond; }); }
另一个文件:respondAppointment.js
const con = require('./../database_connection'); const userDetails = require('./getUserAppointmentDetails'); exports.respondAppointment = function (req, res, next) { const details = req.body; var userData = userDetails.getUserAppointmentDetails(details.id); console.log(userData); con.query('update user_appointment_details SET status = "' + details.status + '" WHERE id = ' + details.id, (err, respond) => { if (err) throw err; res.send({result: respond}); }); }