获取nodejs中另一个控制器中的一个控制器的响应

时间:2017-11-17 07:34:17

标签: node.js

我面临着从另一个控制器内的一个控制器获取响应的问题。

如何从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);
    }

如果有人有任何想法,请回复。

1 个答案:

答案 0 :(得分:0)

这里的问题是你要在两者中发送响应,而不是实际从另一个函数返回结果。

假设您希望getUserAppointmentDetails.js中的结果为respondAppointment.js。在getUserAppointmentDetails.jsgetUserAppointmentDetails.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});
    });


    }