我正在使用NodeJS,Express和Express-handbars从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}}
我希望有足够的信息,如果需要的话,随时请我提供更多信息。
答案 0 :(得分:0)
因此,我使用了sublime并决定切换到Visual Studio代码。 Visual Studio Code Linter回答了我的问题。它告诉我我的等待是无所事事,并建议出于某种未知原因(可能是我尝试其他事情的残余)删除了我在那里的多余功能。通过更改此解决方案:
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文件中使用中间件处理网站图标。