简体   繁体   English

我没有从module.exports得到答案

[英]I do not get an answer from module.exports

db.js db.js

 pool.connect(function (err, client, done) {

client.query('SELECT id, user_id, name  FROM public.chat;', function (err, 
 result) {
    if (err) console.log("error" + err.message);


    else {
        module.exports.res = result.rows[0];
         }
        });

 });

file app.js 文件app.js

 var con = require('./db/connect');

 console.log(con.res);

I'm getting the result undefined ,I don't understand, the reason is callback? 我得到的结果是不确定的,我不明白,原因是回调?

Try this: 尝试这个:

db.js: db.js:

module.exports = pool.connect(function (err, client, done) {
                   client.query('SELECT id, user_id, name  FROM public.chat;', function (err, result) {

                     if (err) 
                       console.log("error" + err.message);
                     else 
                       return result.rows[0];
                     });
                  });

app.js: app.js:

 var con = require('./db/connect');
 console.log(con());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM