2017-04-11 63 views
1

我正在創建一個貓鼬綱要,但我得到一個MongooseErrorMongooseError:投入嵌入失敗值

這是我的計劃:

let RestaurantSchema = new Schema({ 
    ratings: { 
     type: [{ 
      id: Number, 
      value: Number, 
      _id: false 
     }], 
     default: [ 
      { id: 1, value: 0 }, 
      { id: 2, value: 0 }, 
      { id: 3, value: 0 }, 
      { id: 4, value: 0 }, 
      { id: 5, value: 0 } 
     ] 
    }, 
    menu: [{ 
     ratings: { 
      type: [{ 
       id: Number, 
       value: Number, 
       _id: false 
      }], 
      default: [ 
       { id: 1, value: 0 }, 
       { id: 2, value: 0 }, 
       { id: 3, value: 0 }, 
       { id: 4, value: 0 }, 
       { id: 5, value: 0 } 
      ] 
     } 
    }] 
}) 

這是我得到的錯誤:

/var/app/node_modules/mongoose/lib/schema/documentarray.js:322 
      throw new CastError('embedded', valueInErrorMessage, 
      ^
MongooseError: Cast to embedded failed for value "{ id: 5, value: 0 }" at path "rating" 
    at CastError (/var/app/node_modules/mongoose/lib/error/cast.js:26:11) 
    at DocumentArray.cast (/var/app/node_modules/mongoose/lib/schema/documentarray.js:322:19) 
    at DocumentArray.SchemaType.getDefault (/var/app/node_modules/mongoose/lib/schematype.js:616:23) 
    at EmbeddedDocument.Document.$__buildDoc (/var/app/node_modules/mongoose/lib/document.js:265:22) 
    at EmbeddedDocument.Document (/var/app/node_modules/mongoose/lib/document.js:61:20) 
    at EmbeddedDocument [as constructor] (/var/app/node_modules/mongoose/lib/types/embedded.js:31:12) 
    at new EmbeddedDocument (/var/app/node_modules/mongoose/lib/schema/documentarray.js:70:17) 
    at DocumentArray.SchemaArray [as constructor] (/var/app/node_modules/mongoose/lib/schema/array.js:67:21) 
    at new DocumentArray (/var/app/node_modules/mongoose/lib/schema/documentarray.js:31:13) 
    at Function.Schema.interpretAsType (/var/app/node_modules/mongoose/lib/schema.js:643:16) 
    at Schema.path (/var/app/node_modules/mongoose/lib/schema.js:563:29) 
    at Schema.add (/var/app/node_modules/mongoose/lib/schema.js:445:12) 
    at new Schema (/var/app/node_modules/mongoose/lib/schema.js:99:10) 
    at Object.<anonymous> (/var/app/models/restaurant.js:12:24) 
    at Module._compile (module.js:571:32) 
    at Object.Module._extensions..js (module.js:580:10) 
    at Module.load (module.js:488:32) 
    at tryModuleLoad (module.js:447:12) 
    at Function.Module._load (module.js:439:3) 
    at Module.require (module.js:498:17) 
    at require (internal/module.js:20:19) 
    at Object.<anonymous> (/var/app/controllers/restaurant.js:6:18) 
    at Module._compile (module.js:571:32) 
    at Object.Module._extensions..js (module.js:580:10) 
    at Module.load (module.js:488:32) 
    at tryModuleLoad (module.js:447:12) 
    at Function.Module._load (module.js:439:3) 
    at Module.require (module.js:498:17) 

我在做什麼錯?

回答

0

它似乎是在版本4.9.4中的mongoose中的一個錯誤。事實上,還有另一個錯誤產生這一個。後者出現在使用type作爲對象{id: Number,value: Number,_id: false}而不是陣列:

TypeError: this.__parentArray._markModified is not a function 
    at EmbeddedDocument.markModified (/home/user/Desktop/node_modules/mongoose/lib/types/embedded.js:87:24) 
    at SingleNested.Subdocument.markModified (/home/user/Desktop/node_modules/mongoose/lib/types/subdocument.js:62:18) 
    at SingleNested.Document.$__set (/home/user/Desktop/node_modules/mongoose/lib/document.js:874:10) 

這是引用here引用的錯誤。它似乎已經固定了一段時間,但已經重新出現。我已經開了一個關於這個here的新問題。

解決方法是一樣的規定here,替換node_modules/mongoose/lib/types/embedded.js

EmbeddedDocument.prototype.markModified = function(path) { 
    this.$__.activePaths.modify(path); 
    if (!this.__parentArray) { 
    return; 
    } 

EmbeddedDocument.prototype.markModified = function(path) { 
    this.$__.activePaths.modify(path); 
    if (!this.__parentArray || !this.__parentArray._markModified) { 
    return; 
    } 

而且,重塑你的模型爲不同的架構幫助調試問題:

let RatingsItemSchema = new mongoose.Schema({ 
    id: Number, 
    value: Number, 
    _id: false 
}); 

let RatingsItem = db.model('RatingsItem', RatingsItemSchema); 

let RatingsSchema = new mongoose.Schema({ 
    ratings: { 
     type: [RatingsItemSchema], 
     default: [ 
      { id: 1, value: 0 }, 
      { id: 2, value: 0 }, 
      { id: 3, value: 0 }, 
      { id: 4, value: 0 }, 
      { id: 5, value: 0 } 
     ] 
    }, 
    _id: false 
}); 

let RestaurantSchema = new mongoose.Schema({ 
    ratings: { 
     type: [RatingsItemSchema], 
     default: [ 
      { id: 1, value: 0 }, 
      { id: 2, value: 0 }, 
      { id: 3, value: 0 }, 
      { id: 4, value: 0 }, 
      { id: 5, value: 0 } 
     ] 
    }, 
    menu: { 
     type: [RatingsSchema] 
    } 
}); 

let Ratings = db.model('Ratings', RatingsSchema); 
let Restaurant = db.model('Restaurant', RestaurantSchema); 

let rest = new Restaurant(); 
rest.menu.push(new Ratings()); 
console.log(JSON.stringify(rest, null, 2)); 
+0

解決方法有效,但有點惱人ge mongoose文件。我會等待可能解決此問題的新版貓鼬。非常感謝你。 –