[英]Sending a SELECT sql variable array from one file to another in Nodejs
我正在尝试从此功能将module.export“行”转移到anotherfile.js
“ mysql.js”
const gameserverShow = function () {
con.query(`SELECT * FROM gameservers`, (err, rows) => {
if (err) throw err;
return rows;
});
}
module.exports.gameserverShow = gameserverShow;
我试图将“ foreach循环”放入数组并发送过来,但是每次我尝试console.log时,它都会返回“ undefined”。 有其他/正确的方法吗?
“ anotherfile.js”
const mysqlcon = require("./mysql.js");
if (args[0] == 'show') {
mysqlcon.gameserverShow();
console.log(mysqlcon.gameserverShow.rows);
}
您这样做的方式有误。 您首先应该阅读javascript函数调用,回调的工作方式以及如何处理javascript中的异步任务。 您必须使用回调或Promise异步处理它。 当您在这里使用回调时,以下是该代码的基本代码。
const gameserverShow = function (callback) {
con.query(`SELECT * FROM gameservers`, (err, rows) => {
if (err) callback(err);
callback(null, rows);
});
}
module.exports.gameserverShow = gameserverShow;
并在另一个文件中
const mysqlcon = require("./mysql.js");
if (args[0] == 'show') {
mysqlcon.gameserverShow((error, rows) => {
console.log(rows);
});
}
您可以搜索如何用诺言做到这一点。 这里有很多问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.