gpt4 book ai didi

javascript - NodeJS 变量未定义

转载 作者:行者123 更新时间:2023-12-01 01:41:25 29 4
gpt4 key购买 nike

我似乎找不到为什么这不起作用的问题。当我转到 localhost:3000 时,出现此错误 ReferenceError: pixwords is not defined

app.get('/', (req, res) => {
Word.find().then((pixwords) => {
if(!pixwords) {
return res.send('No text available');
}
});
res.render('index.hbs', {pixwords});
});

这是index.hbs:

{{#each pixwords}}
{{this.text}}
{{/each}}

最佳答案

pixwords仅在Word.find().then()的回调函数中定义,您只能在那里使用它:

app.get('/', (req, res) => {
Word.find().then((pixwords) => { // <-- this declares pixwords
if (!pixwords) {
return res.send('No text available');
}
res.render('index.hbs', { // <-- move the call in the .then
pixwords
});
});
// anything here is executed before Word.find has finished running
});

关于javascript - NodeJS 变量未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52368251/

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