Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用mongoose的对象引用_Javascript_Html - Fatal编程技术网

Javascript 使用mongoose的对象引用

Javascript 使用mongoose的对象引用,javascript,html,Javascript,Html,var express=require(“express”); var mongoose=要求(“mongoose”); //注释模式 var commentsSchema=newmongoose.Schema({ 文本:字符串 }); var comment=mongoose.model(“文本”,commentschema); //后模式 var postschema=newmongoose.Schema({ 帖子:String, 文本:[ { 类型:mongoose.Schema.Type

var express=require(“express”);
var mongoose=要求(“mongoose”);
//注释模式
var commentsSchema=newmongoose.Schema({
文本:字符串
});
var comment=mongoose.model(“文本”,commentschema);
//后模式
var postschema=newmongoose.Schema({
帖子:String,
文本:[
{
类型:mongoose.Schema.Types.ObjectId,
参考:“文本”
}
]
});
var posts=mongoose.model(“post”,postschema);
//邮路
app.get('/home',函数(req,res){
find({},函数(err,posts){
如果(错误){
控制台日志(err);
}否则{
//控制台日志(posts);
res.render(“index.ejs”,{posts:posts});
}
});
});
//邮路
应用程序post('/home',功能(请求、回复){
创建({posts:req.body.post,username:req.user.username},函数(err,post){
如果(错误){
控制台日志(err);
}否则{
console.log(“新创建的帖子”);
res.redirect(“/主页”);
}
});
});
//注释路径:-在这个路径中,我创建了推送到post模式中的注释
app.post('/home/:id/comments',函数(req,res){
posts.findById(请求参数id,函数(err,post){
如果(错误){
控制台日志(err);
}否则{
var com=req.body.comment;
var user=req.user.username;
console.log(com);
var com1={text:com,username:user};
comment.create(com1,函数(err,newComment){
如果(错误){
控制台日志(“err”);
}否则{
console.log(newComment);
post.text.push(新来者);
post.save();
控制台日志(post);
res.redirect(“/主页”);
}
});
}
});
});

post.text.forEach(函数(注释){
//错误:-显示对象id而不是文本
});

您需要填充子架构(文本)

请仔细阅读这份文件


您需要填充子架构(文本)

请仔细阅读这份文件

如果您使用的是mongoose“ref”属性,那么您必须使用mongoose查询和find查询来用注释模式填充post模式。试试看,这肯定会解决你的问题。

如果你使用mongoose“ref”属性,那么你必须使用mongoose查询和find查询来用注释模式填充post模式。试试看,这肯定会解决你的问题

posts.find({}).
  populate('texts').
  exec(function (err, posts) {...
});