2016-01-23 45 views
1

我目前正在学习Mean Mean教材的Mean Stack。当我在locationSchema上使用子文档时遇到此问题。它必须是与ref财产有关的东西。我如何设法解决这个问题?Mongodb给出了嵌套模式的错误

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) 
+1

在name的定义中,它应该是'type:String',而不是'Type:string'。 – JohnnyHK

+0

哇错字。 :o谢谢。 – Bun

回答

3

错字:○name属性:

var locationSchema = new mongoose.Schema({ 
    name: { 
     Type: String, 
     required: true 
    }, 

    openingTimes: [openingTimeSchema], 
    reviews: [reviewSchema] 
}); 

它应该是:

name: { 
    type: String, 
    required: true 
},