我试图弄清楚为什么我的架构数组引用会为数组行mongoose.Schema.Types.ObjectId
引发错误。我试图用这个数组引用我的第二个集合模型,但我得到了Cannot read property Types of undefined
这是我的错误:
imagesModel.js:10
description_id: { type: mongoose.Scehma.Types.ObjectId, ref: 'Description'}
^
TypeError: Cannot read property 'Types' of undefined
以下是模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var descriptionSchema = new Schema({
pattern: String,
color: String,
body: String,
images: [{type: mongoose.Schema.Types.ObjectId, ref: 'Images'}]
});
var Description = mongoose.model('Description', descriptionSchema);
module.exports = Description;
以下是作为参考的图像模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var imageSchema = new Schema({
pattern: String,
color: String,
imageName: String,
imageUrl: String,
imageSource: String,
description_id: { type: mongoose.Scehma.Types.ObjectId, ref: 'Description'}
});
imageSchema.pre('save', function(next){
var Description = require("../models/descriptionModel");
var image = this;
Description.findOneAndUpdate({
_id: {$in: {image.description_id}},
{$push: {images: image._id}},
next
});
});
var Images = mongoose.model('Images', imageSchema);
module.exports = Images;