我的开发服务器崩溃了,无法重启。当我查看日志时,收到以下消息。下面是错误
from itertools import count
for i in count():
print(i)
# don't run this without some mechanism to break the loop, i.e.
# if i == 10:
# break
# OUTPUT
# 0
# 1
# 2
# ...and so on
此消息仅在生产中出现,而不在开发中出现,这使我感到困惑。这是来自app / routes / index.js文件的相关代码
throw er; // Unhandled 'error' event
TypeError: Cannot read property '_id' of null
at /app/routes/index.js:220:53
at model.Query.<anonymous>(/app/node_modules/mongoose/lib/model.js:4092:16)
at /app/node_modules/kareem/index.js:273:21
at /app/node_modules/kareem/index.js:131:16
at process._tickCallback (internal/process/next_tick.js:61:11)
Emitted 'error' event at:
at model.Query.<anonymous(/app/node_modules/mongoose/lib/model.js:4094:13)
at /app/node_modules/kareem/index.js:273:21
at /app/node_modules/kareem/index.js:131:16
at process._tickCallback (internal/process/next_tick.js:61:11)
我在下面包括了我的评论架构。我尝试使用此解决方案node js returns undefined from mongodb obejct inner value,但不适用于这种情况。
// USER PROFILE
router.get("/users/:id", function(req, res) {
User.findById(req.params.id, function(err, foundUser) {
if(err) {
req.flash("error", "Something went wrong.");
res.redirect("/");
}
Line 220 Comment.find().where('author').equals(foundUser._id).exec(function(err, comments) {
if(err) {
req.flash("error", "Something went wrong.");
res.redirect("/");
}
res.render("show", {user: foundUser, comments: comments});
})
});
});
编辑:基于评论中的讨论,我进行了一些测试,使我也能发现开发中出现的错误。 我console.logged输出,这就是我得到的。
var mongoose = require("mongoose");
var CommentSchema = mongoose.Schema({
text: String,
created:
{type: Date, default: Date.now},
author: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
blog: String
});
// pre-hook middleware to populate author in comment show route
CommentSchema.pre('findOne', function(next) {
this.populate('author');
next();
});
module.exports = mongoose.model("Comment", CommentSchema);
当时我以管理员用户身份登录。然后,我测试了console.log(req.params)
This is the id { _id: 5af9af45f5a3b31bd4492e9d,
username: 'mrboston',
firstName: 'Mr',
lastName: 'Boston',
email: 'fake@yahoo.com',
avatar: 'google.com',
__v: 0,
isAdmin: true }
This is the id undefined
events.js:160
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property '_id' of undefined
这是否意味着我对req.params查询的了解不够具体?
编辑2:
代码已更新为此:
{ id: '5af9af45f5a3b31bd4492e9d' }
{ id: 'google.com' }
events.js:160
throw er; // Unhandled 'error' event
但是运行该应用程序并单击显示用户链接,我会收到此错误消息
// USER PROFILE
router.get("/users/:id", function(req, res) {
User.findById(req.params.id, function(err, foundUser) {
if(err) {
req.flash("error", "Something went wrong.");
res.redirect("/");
}
if (!foundUser) {
Comment.find().where('author').equals(foundUser._id).exec(function(err, comments) {
if(err) {
req.flash("error", "Something went wrong.");
res.redirect("/");
}
res.render("show", {user: foundUser, comments: comments});
});
}
});
});
编辑3:UserSchema
<html>
<head>
<meta charset='utf-8'>
<title>Error 502 - Bad Gateway</title>
<link rel="stylesheet" type="text/css" href="https://cdn.c9.io/errors/style.css" />
<style type="text/css">
.error_content {
background: rgba(255, 255, 255, 0.23);
padding: 10px;
width: 641px;
margin: 25px 0;
display: none;
}
#error-msg {
display: block;
}
</style>
</head>
<body class="errorUnknown light">
<div id="wrapper">
<h1>Error 502 - Bad Gateway</h1>
<div class="error_content" id="error-msg">
<p>Please click <a href="javascript:location.reload(true)">here</a> to try again, if the issue persists please contact <a href="https://c9.io/support">support</a></p>
</div>
<a href="http://status.c9.io">Status Page</a> |
<a href="https://c9.io/support">Support</a> |
<a href="https://c9.io/dashboard.html">Dashboard</a> |
<a href="https://c9.io">Home</a>
</div>
</body>
答案 0 :(得分:1)
我没有足够的声誉来发表评论,但我相信当您在控制台日志中返回2个ID时发生错误。 502错误表示您不介意为用户发布您的架构,因为从那里2个ID正在以某种方式返回。
编辑:2 我想我可能已经找到了您的问题(不确定),但是在预钩中间件中
CommentSchema.pre('findOne', function(next) {
this.populate('author');
next();
});
此处您使用的是 findOne ,但在途中您使用的是 find 方法,这就是为什么它没有填充您的用户ID并返回 null 的原因。这是更新的版本。请尝试
router.get("/users/:id", function(req, res) {
User.findById(req.params.id, function(err, foundUser) {
if(err) {
req.flash("error", "Something went wrong.");
res.redirect("/");
}
if (foundUser) {
Comment.findOne().where('author').equals(foundUser._id).exec(function(err, comments) {
if(err) {
req.flash("error", "Something went wrong.");
res.redirect("/");
}
res.render("show", {user: foundUser, comments: comments});
});
}
if(!foundUser){req.flash("error","User Not Found")}
让我知道是否可以解决您的错误
答案 1 :(得分:0)
基于Shivam的评论,我将代码更新为:
if (foundUser) {
Comment.find().where('author').equals(foundUser._id).exec(function(err, comments) {
if(err) {
req.flash("error", "Something went wrong.");
res.redirect("/");
}
res.render("show", {user: foundUser, comments: comments});
});
}
});
});
这有效。谢谢大家的帮助