gpt4 book ai didi

node.js - Node js 导出模块在导入它的另一个文件中返回未定义的值

转载 作者:太空宇宙 更新时间:2023-11-03 22:07:29 25 4
gpt4 key购买 nike

我在 console.log 中未定义。为什么?如何纠正它,我必须只针对这个原因这样做。

这是演示文件的代码:

exports.selectorquery = function() {
var conn = "New connection";
var nt = "Last Connect";

function myFunc(arg) {
return conn;
}

setTimeout(myFunc, 3500, 'funky');
}

这是用于 run.js 的:

var go = require('./demofile.js');
console.log(go.selectorquery());

最佳答案

您的函数没有返回任何内容:

具体操作方法如下。

demofile.js

exports.selectorquery = function (callback) {
var conn = "New connection";
var nt = "Last Connect";
function myFunc(arg) {
callback(null, conn);

}
setTimeout(myFunc, 3500, 'funky');
}

run.js

const go = require('./demofile');

go.selectorquery(function(err, data) {
console.log(data);
});

现在运行:

node run.js

它应该记录

New connection

了解更多:NodeJS Export and Import Modules

希望它能解决您的疑问。

关于node.js - Node js 导出模块在导入它的另一个文件中返回未定义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51513226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com