网页呈现,但第一次使服务器崩溃,然后在不使服务器崩溃的情况下进行后续呈现

Web page renders, but crashes the server the first time, then renders without crashing the server subsequent times

我正在使用 NodeJS、Express 和 Express-handlebars 使用从 SQLITE3 数据库中提取的数据呈现网页。

代码有效,页面呈现正确,但第一次加载时,它使我的服务器崩溃并出现以下错误:TypeError: Cannot read property 'user_name_tag' of undefined

错误说它无法读取这个 属性,但是在服务器崩溃之前正确地显示了它的信息。但是,如果重新启动服务器并重新加载页面,它不会崩溃,如果我在初始加载后打开一个具有不同用户和不同数据集的新实例,它也不会崩溃。

这是 index.js 代码:

const express = require('express');
const app = express();
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('../database.sqlite');
const exphbs = require('express-handlebars');
const hbs = exphbs.create({
    defaultLayout: 'main',
//custom helpers
    helpers: {
        iff: function(a, b, opts) {
            if (a == b) {
                return opts.fn(this);
            } else {
                return opts.inverse(this);
            }
        }
    }
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

app.get('/:userid', async (reg, res) => {
    const findUser = reg.params.userid;
    console.log('I am findUser ',findUser);
      let query = "SELECT users.user_id, users.balance, users.user_avatar, users.user_name_tag, user_collections.user_stamina, user_collections.collection_switch,";
      query += "COALESCE((SELECT SUM((SELECT (currency_shops.cost * user_items.amount) FROM currency_shops WHERE currency_shops.id = user_items.item_id)) FROM user_items WHERE user_items.user_id = users.user_id GROUP BY user_items.user_id), 0) as totalItems, ";
      query += "COALESCE((SELECT SUM((SELECT (craft_shops.cost * user_craft_items.amount) FROM craft_shops WHERE craft_shops.id = user_craft_items.item_id)) FROM user_craft_items WHERE user_craft_items.user_id = users.user_id GROUP BY user_craft_items.user_id), 0) as totalCraftItems, ";
      query += "COALESCE((SELECT SUM((SELECT (stonk_shops.cost * user_stonks.amount) FROM stonk_shops WHERE stonk_shops.id = user_stonks.item_id)) FROM user_stonks WHERE user_stonks.user_id = users.user_id GROUP BY user_stonks.user_id), 0) as totalStonks ";
      query += "FROM users, user_collections WHERE users.user_id = ? AND users.user_id = user_collections.user_id";
    await db.get(query, [findUser], (err, row ) => {
            (err, rows) => {       
        };
                res.render('profile', {
                title: 'User Profile',
                user_name: ([row.user_name_tag]),
                user_id:([row.user_id]),
                user_balance:([row.balance]),
                user_avatar:('src','https://cdn.discordapp.com/avatars/' + [row.user_id] + '/' + [row.user_avatar] + '.png'),
                user_stamina:([row.user_stamina]),
                user_networth:([row.balance + row.totalItems + row.totalCraftItems + row.totalStonks]),
                collection_switch:([row.collection_switch]),    
                isCollectionOn:([row.collection_switch]),
            }) 

   });

});
const PORT = process.env.PORT || 6999; 
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));

这里是 HTML:

<h1>Welcome to your profile page {{user_name}} !</h1>
<h1>This is your ID number {{user_id}} .</h1>
<h1>This is your current credits balance {{user_balance}} .</h1>
<h1><img src='{{user_avatar}}'>This is your face!</img></h1>
<h1>This is your current stamina {{user_stamina}} .</h1>
<h1>This is your networth {{user_networth}}</h1>
{{#iff isCollectionOn 0}}
<h1>Your collection switch is set to {{collection_switch}}, you are free to collect!</h1>
{{else}}
<h1>This worked, congrats!</h1>
{{/iff}}

我希望这些信息足够了,如果需要,请随时让我提供更多信息。

所以我在使用 sublime 并决定切换到 visual studio 代码。 Visual studio codes linter 回答了我的问题。它告诉我我的 await 什么也没做,并建议删除我在那里的一个冗余功能,原因不明(可能是我尝试不同事情的遗留物)。它通过改变这个解决了:

await db.get(query, [findUser], (err, row ) => {
            (err, rows) => {       
        };
                res.render('profile', {

对此

    db.get(query, [findUser], (err, row ) => {
            res.render('profile', {

另外,我应该注意,我的网站图标似乎与我遇到的一些崩溃有关。我发现使用 Express 时,favicons 是通过 index.js 文件而不是 html 文件中的中间件处理的。