Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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
Node.js Mongoose填充虚拟不工作并返回null_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js Mongoose填充虚拟不工作并返回null

Node.js Mongoose填充虚拟不工作并返回null,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我读过,觉得它很酷,它类似于SQL中的连接操作 当我在我的应用程序上尝试它时,它不起作用。我的应用程序与此类似,只是简化了 我在单独的文件中有一个教室和教师模式 教室.js ... ... var ClassroomSchema = new Schema({ class_code:String, teacher_id:String }); ClassroomSchema.virtual('teacher',{ ref:'Teacher', //mode

我读过,觉得它很酷,它类似于SQL中的连接操作

当我在我的应用程序上尝试它时,它不起作用。我的应用程序与此类似,只是简化了

我在单独的文件中有一个教室和教师模式

教室.js

...
...
var ClassroomSchema = new Schema({
   class_code:String,
   teacher_id:String
});

ClassroomSchema.virtual('teacher',{
   ref:'Teacher',             //model to reference
   localField:'teacher_id',   //Class.teacher_id
   foreignField:'teacher_id', //a Teacher.teacher_id
   justOne:true
});

module.exports = mongoose.model('Classroom',ClassroomSchema');
***
***
var TeacherSchema = new Schema({
   name:String,
   teacher_id:String
});

module.exports = mongoose.model('Teacher',TeacherSchema);
var Classroom = require('Classroom.js');
var Teacher = require('Teacher.js');

//Find classrooms
Classroom.find({}).populate('teacher').exec(function(err,docs){
    if(err) throw err
    if(docs) console.log(docs);
});
Teacher.js

...
...
var ClassroomSchema = new Schema({
   class_code:String,
   teacher_id:String
});

ClassroomSchema.virtual('teacher',{
   ref:'Teacher',             //model to reference
   localField:'teacher_id',   //Class.teacher_id
   foreignField:'teacher_id', //a Teacher.teacher_id
   justOne:true
});

module.exports = mongoose.model('Classroom',ClassroomSchema');
***
***
var TeacherSchema = new Schema({
   name:String,
   teacher_id:String
});

module.exports = mongoose.model('Teacher',TeacherSchema);
var Classroom = require('Classroom.js');
var Teacher = require('Teacher.js');

//Find classrooms
Classroom.find({}).populate('teacher').exec(function(err,docs){
    if(err) throw err
    if(docs) console.log(docs);
});
在myapp.js中

...
...
var ClassroomSchema = new Schema({
   class_code:String,
   teacher_id:String
});

ClassroomSchema.virtual('teacher',{
   ref:'Teacher',             //model to reference
   localField:'teacher_id',   //Class.teacher_id
   foreignField:'teacher_id', //a Teacher.teacher_id
   justOne:true
});

module.exports = mongoose.model('Classroom',ClassroomSchema');
***
***
var TeacherSchema = new Schema({
   name:String,
   teacher_id:String
});

module.exports = mongoose.model('Teacher',TeacherSchema);
var Classroom = require('Classroom.js');
var Teacher = require('Teacher.js');

//Find classrooms
Classroom.find({}).populate('teacher').exec(function(err,docs){
    if(err) throw err
    if(docs) console.log(docs);
});
然后返回教室,但“教师”字段等于null


我是不是遗漏了什么?为什么返回null?

将toJson和toObject选项添加到模式中,如下所示:

var ClassroomSchema = new Schema({
   class_code:String,
   teacher_id:String
},
{ toJSON: { virtuals: true }, toObject: { virtuals: true }});

将toJson和toObject选项添加到模式中,如下所示:

var ClassroomSchema = new Schema({
   class_code:String,
   teacher_id:String
},
{ toJSON: { virtuals: true }, toObject: { virtuals: true }});

它在5.2.18之后的版本上不起作用。
如果将virtuals设置为true,则只有虚拟字段;如果将virtuals设置为false,则有虚拟填充,但没有字段

它在5.2.18之后的版本上不起作用。
如果将virtuals设置为true,则只有虚拟字段;如果设置为false,则有虚拟填充字段,但没有字段

在测试哪个版本的MongoDB?使用哪个版本的mongoose?我用
5.0.1
尝试了你的代码,它运行正常。您是否也尝试过打印
文档[0]。教师
而不是
控制台.log(文档)
?默认情况下,在检查/jsonizing objects.MongoDB 3.6.3和Mongoose 5.0.13时跳过虚拟现实。这不是我的实际应用程序,只是一个简化,我的实际应用程序有额外的字段,但本质上是相同的。我只是想知道它不起作用的原因是什么。我应该发布我的实际应用程序吗?它相当长,因为它应该是某种学校系统。另外,我还没有尝试单独打印它,我会尝试。我尝试了console.log(docs[0].teacher)但它不起作用,它会生成一个错误“无法读取未定义的属性'teacher'。如果
justOne
为true,“docs”将是单个文档,而不是数组<默认情况下,代码>justOne
为false。在您的示例中,是“true”,因此“docs”不是数组。试试console.log(docs.teacher)你在测试哪个版本的MongoDB?你在使用哪个版本的mongoose?我用
5.0.1
尝试了你的代码,它运行正常。您是否也尝试过打印
文档[0]。教师
而不是
控制台.log(文档)
?默认情况下,在检查/jsonizing objects.MongoDB 3.6.3和Mongoose 5.0.13时跳过虚拟现实。这不是我的实际应用程序,只是一个简化,我的实际应用程序有额外的字段,但本质上是相同的。我只是想知道它不起作用的原因是什么。我应该发布我的实际应用程序吗?它相当长,因为它应该是某种学校系统。另外,我还没有尝试单独打印它,我会尝试。我尝试了console.log(docs[0].teacher)但它不起作用,它会生成一个错误“无法读取未定义的属性'teacher'。如果
justOne
为true,“docs”将是单个文档,而不是数组<默认情况下,代码>justOne
为false。在您的示例中,是“true”,因此“docs”不是数组。试试console.log(docs.teacher)