Using exports in nodejs to return a value from function in a function

谁说胖子不能爱 提交于 2019-12-11 18:40:07

问题


I've been doing some reading on modularizing my code and decided to export some functions to a separate file and include them in my main function once it's called. Only my config of my website is not returning if I'm calling it:

// Export from my controller
// File: Controller.js
exports.site_config = function(company, data) {

   siteConfig.find({"company" : company}, function data (err, siteConfig, data) {
    // Console.log(siteConfig[0]) // Works
    return siteConfig[0] // This return is not working
    })
  // Only here returns works....
}

// File: Index.js
const siteController = require('../controllers/site');
console.log(siteController.site_config('company2')) // nothing return




回答1:


your find function return a callback you can do something like this:

// Export from my controller
// File: Controller.js
exports.site_config = function(company, data, callback) {

   siteConfig.find({"company" : company}, function (err, siteConfig, data) {
    callback(siteConfig[0]);
    })
}

// File: Index.js
const siteController = require('../controllers/site');
console.log(siteController.site_config('company2', null,function (result) {
  console.log(result)
})) 



回答2:


In nodejs- express js , you need to pass function as callback .

when result is available or some error occurred in code return them in callback;

// File: Controller.js

write your function inside module.exports :

module.exports = {
   var site_config = function(company,callback) {
      siteConfig.find({"company" : company}, function(err, data) 
      {
         if(!err && data){
          // no error and data is available , so pass err as null and data as data[0] to callback
         return callback(null,data[0]);
         }else{
          // error occured , pass err as err and data = null to callback
          return callback(err,null);
         }
      });
   }
}

in you File: Index.js , require file Controller.js

const siteController = require('../controllers/Controller');
siteController.site_config('company2',function(err,data){
    console.log(data);
});



回答3:


This is a special case of this problem. Synchronous code can be transformed to asynchronous but not vice versa.

Mongoose has been supporting promises for a long time, callback API is legacy. This is a use case for them.

A module should export a promise of configuration object. A more concise way to retrieve a single document is findOne:

exports.site_config = function(company, data) {
   return siteConfig.findOne({"company" : company});
};

And is consumed as a promise. There should be promise chain, up to to application entry point if needed:

// inside `async` function:
const siteController = require('../controllers/site');
const config = await siteController.site_config('company2');


来源:https://stackoverflow.com/questions/54690224/using-exports-in-nodejs-to-return-a-value-from-function-in-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!