【发布时间】:2018-02-10 10:55:54
【问题描述】:
我正在尝试获取单个用户的详细信息,包括他通过网站制作的所有 cmets(来自评论模式),但是每当我运行查询时在显示路线上,我都会得到 TypeError:(中间值).populate 不是函数。我在互联网和 stackoverflow 上搜索了很多相同的错误消息,但没有运气
显示路线
// profile page search
app.get("/:username", function(req, res) {
User.findOne({ username: username }.populate("comments").exec(function(err, user) {
if (err) {
console.log(err);
res.redirect("error");
} else {
if (user === null)
res.render("invalid");
else
res.render("profile", { user: user });
}
}));
});
用户架构
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var UserSchema = new mongoose.Schema({
id: String,
fullname: String,
username: String,
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
]
});
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSchema);
评论架构
var mongoose = require("mongoose");
var commentSchema= mongoose.Schema({
text: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
});
module.exports = mongoose.model("comments", commentSchema);
我完全迷路了,请帮助 提前致谢
【问题讨论】:
-
{ username: username }.populate看起来不对。为什么对象字面量会有populate方法?
标签: node.js mongodb mongoose mongoose-schema mongoose-populate