node.js - 网页呈现,但第一次使服务器崩溃,然后呈现而不会使服务器随后崩溃
问题描述
我正在使用 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 代码 linter 回答了我的问题。它告诉我我的 await 什么也没做,并建议删除我在那里的一个冗余函数,原因不明(可能是我尝试不同事物的剩余部分)。它通过改变这个来解决:
await db.get(query, [findUser], (err, row ) => {
(err, rows) => {
};
res.render('profile', {
对此
db.get(query, [findUser], (err, row ) => {
res.render('profile', {
另外,我应该注意,我的网站图标似乎与我遇到的一些崩溃有关。我发现使用 Express,网站图标由 index.js 文件中的中间件处理,而不是 html 文件。
推荐阅读
- r - 无法投影多边形和点图层以计算提取
- python - Django Viewset 检索不适用于默认路由器
- c - 通过取消引用将字符串与 uint32_t 进行比较
- python - numpy 数组的平均值比实际平均值小多少?
- reactjs - 不要将 tailwindcss 应用于某些反应组件
- c++ - 为什么 consteval/constexpr 和模板元函数之间的编译时间有如此大的差异?
- networking - 如何在 linux 中测试以太网驱动程序上的多个队列
- typescript - 打字稿认为对象可能未定义,即使它已定义
- http - 两个arduino设备ESP8266之间的通信
- c++ - 带有clang -O3优化的ARM(Apple M1)上的模运算错误结果