在nodejs/express 中,如何防止 if 条件在 readdir 完成之前执行?
app.get('/students/:record_id', function (req, res) {
var found =false;
var record_id = req.params.record_id;
var dirname = "students/";
filenames = fs.readdirSync(dirname);
filenames.forEach(filename => {
fs.readFile(dirname + filename, "utf-8", function (err, data ) {
if (err) {
onError(err);
return;
}
if (data.includes(record_id)) {
found = true;
return res.status(200).send(data);
}
});
});
if(found == false){
return res.status(500).send({ "message": "error - no student found" });
}
});
我正在使用 GET 按学生姓名搜索来检索学生文件。如果我删除 if 条件,则成功。但如果学生不存在,那么它就不起作用。 if 条件通过在文件中找到 find 时将其设为 true 来修复此问题。问题在于 if 条件在读取文件之前执行。 我尝试使用回调,但经过几个小时的研究,我无法弄清楚如何在其中实现它,因为 readfile 回调用于检索学生信息。
我尝试使用承诺,但只有找到学生时,承诺才会兑现,而我不知道如何在找不到学生时兑现承诺。
app.get('/students/:record_id', function (req, res) {
var found =false;
var record_id = req.params.record_id;
var dirname = "students/";
filenames = fs.readdirSync(dirname);
filenames.forEach(filename => {
fs.readFile(dirname + filename, "utf-8", function (err, data ) {
if (err) {
onError(err);
return;
}
if (data.includes(record_id)) {
found = true;
return res.status(200).send(data);
}
});
});
if(found == false){
return res.status(500).send({ "message": "error - no student found" });
}
});
I am using GET searching by a student name to retrieve a students file. If I remove the if condition it is successful. But if the student does not exist then it does not work.
The if condition fixes this by making found true when it finds it in the file. The problem is that the if condition executes before the file is read.
I tried using a callback but after a couple hours of research I can not figure out how to implement it in this since the readfile callback is being used to retrieve student info.
I tried using a promise but the promise is then only fulfilled if the student is found and I do not know how to implement it where it is fulfilled when it is not found.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
既然您已经尝试过使用 Promises,我将向您展示如何使用 Promises
使用 async/await 尤其使这变得非常简单
注意:因为我不知道您如何使用
fs
导入/require 只获取 readdir 和 readFile 的承诺版本,并像readdir
而不是fs.readdir
使用它 - 所以其他现有代码不会破坏注意:我不会'发送 500 (内部服务器错误)如果找不到学生 - 我更改了逻辑以发送 404(未找到) - 这更合适
如果您想要纯粹的承诺(无异步/等待) - 我相信以下是一种方法它,可能不是最好的代码,但它应该可以工作(虽然还没有测试过)
并且,最后 - 根本不使用 Promises
Since you've tried with Promises, I'll show you how to do it with Promises
Using async/await especially makes this really easy
Note: since I don't know how you are using
fs
the import/require only gets the promise versions of readdir and readFile, and uses it likereaddir
rather thanfs.readdir
- so other existing code won't breakNote: I wouldn't send a 500 (Internal Server Error) if a student is not found - I changed the logic to send 404 (not Found) instead - which is more appropriate
If you want pure promise (no async/await) - I believe the following is one way to do it, probably not the nicest code, but it should work (haven't tested though)
And, finally - without using Promises at all