require函数的返回数据变得未定义(Js,NodeJs,Discord.js)[duplicate](Returned data of require function becomes undef

编程入门 行业动态 更新时间:2024-10-27 23:26:49
require函数的返回数据变得未定义(Js,NodeJs,Discord.js)[duplicate](Returned data of require function becomes undefined (Js, NodeJs, Discord.js) [duplicate])

这个问题在这里已经有了答案:

如何返回来自异步调用的响应? 33个答案

在同一个文件夹中,我有3个不同的文件,内容如下: •index.js

const getToken = require('./getToken.js'); console.log(getToken.getToken());

getToken.js

const FS = require('fs'); //a library to read files module.exports = { getToken: function (){ FS.readFile('./token.txt', 'utf8', function (err, data) { if (err) { console.error(err); } else { console.log('Token is : ' + data); return data; } }); } }

•token.txt

foo

我使用NodeJs,fs,一个库来读取文件和一个Discord库。 我得到输出:

undefined //result of index.js Token is : foo //result of getToken.js

代码工作正常,除了令牌没有正确地给予主js文件(index.js)即使它被正确读取。 这段代码有什么问题? 为什么console.log()的结果是错误的?

谢谢。

This question already has an answer here:

How do I return the response from an asynchronous call? 35 answers

In the same folder I have 3 different files with the following content : • index.js

const getToken = require('./getToken.js'); console.log(getToken.getToken());

• getToken.js

const FS = require('fs'); //a library to read files module.exports = { getToken: function (){ FS.readFile('./token.txt', 'utf8', function (err, data) { if (err) { console.error(err); } else { console.log('Token is : ' + data); return data; } }); } }

• token.txt

foo

I'm using NodeJs, fs, a library to read files and a Discord library. I get as output :

undefined //result of index.js Token is : foo //result of getToken.js

The code is working fine except that the token isn't given correctly to the main js file (index.js) even if it's read properly. What's wrong with this code? Why do the results of console.log() are in the wrong ordrer?

Thanks.

最满意答案

欢迎来到异步世界。 在异步操作(fs.readFile为异步)后返回值时,您需要使用回调或承诺;

使用回调将如下所示:

module.exports = { getToken: function (callback){ FS.readFile('./token.txt', 'utf8', function (err, data) { if (err) { callback(err); } else { console.log('Token is : ' + data); callback(null, data); } }); } } //Other file const getToken = require('./getToken.js'); getToken.getToken(function(err, result){ if(err) return console.log(err); console.log(result); });

Welcome to the async world. You need to use callbacks or promises when returning values after an async operation (fs.readFile is async);

Using callback would be like this:

module.exports = { getToken: function (callback){ FS.readFile('./token.txt', 'utf8', function (err, data) { if (err) { callback(err); } else { console.log('Token is : ' + data); callback(null, data); } }); } } //Other file const getToken = require('./getToken.js'); getToken.getToken(function(err, result){ if(err) return console.log(err); console.log(result); });

更多推荐

本文发布于:2023-07-04 10:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023093.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   未定义   数据   NodeJs   Js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!