我目前正在从Getting Mean教科书中学习Mean Stack。我在locationSchema
上使用子文档时遇到了这个问题。它必须与ref
属性相关。我该如何解决这个问题?
var mongoose = require('mongoose');
// reviewSchema
var reviewSchema = new mongoose.Schema({
author: String,
});
var openingTimeSchema = new mongoose.Schema({
days: {
type: String,
required: true
}
});
// We use openingTimeSchema as a subdocument here
var locationSchema = new mongoose.Schema({
name: {
Type: String,
required: true
},
openingTimes: [openingTimeSchema],
reviews: [reviewSchema]
});
// Build Mongose Schema to models
mongoose.model('Location', locationSchema);
Error
:
TypeError: Undefined type `undefined` at `name.required`
Did you try nesting Schemas? You can only nest using refs or arrays.
at Function.Schema.interpretAsType (loc8r/node_modules/mongoose/lib/schema.js:592:11)
at Schema.path (loc8r/node_modules/mongoose/lib/schema.js:499:29)
at Schema.add (loc8r/node_modules/mongoose/lib/schema.js:389:12)
at Schema.add (loc8r/node_modules/mongoose/lib/schema.js:384:14)
at new Schema (loc8r/node_modules/mongoose/lib/schema.js:92:10)
at Object.<anonymous> (loc8r/app_server/models/locations.js:30:22)
答案 0 :(得分:3)
name
属性
var locationSchema = new mongoose.Schema({
name: {
Type: String,
required: true
},
openingTimes: [openingTimeSchema],
reviews: [reviewSchema]
});
应该是:
name: {
type: String,
required: true
},