2015-09-24 15 views
12

In sostanza sto solo cercando di aggiungere un nuovo sub-documento a mio documento MongoDB esistente che ha il seguente schemaAggiunta di documento secondario al documento esistente MongoDB

/models/server/destination.js

// this is the "destination" model for mongoose 
var mongoose = require('mongoose') 
var Adventure = require('../models/adventure') 

// this is the schema that every entry will get when a new trip is made. 
var tripSchema = mongoose.Schema({ 
    name: { type: String, required: true }, 
    city: { type: String, required: true }, 
    dateStart: { type: Date, required: true }, 
    dateFinish: { type: Date, required: true }, 
    adventures: [Adventure] 
}) 

// module.exports makes this model available to other file 
module.exports = mongoose.model('Destination', tripSchema) 

/server/models/adventure.js

var mongoose = require('mongoose') 

var adventure = mongoose.Schema({ 
    site: String, 
    rating: String, 
    photo: String, 
    website: String, 
    date: Date 
}) 

module.exports = mongoose.model('Adventure', adventure) 

percorso REST per inviare alle avventure

app.post('/api/destinations/:id/addAdventures', destinationsController.addAdventures) 

/server/controllers/controller.js

module.exports.addAdventures = function(req, res) { 
    var id = req.params.id; 
    Destination.findOne({ _id: id }, function(err, result) { 
     var adventure = new Adventure(req.body) 
     var destination = result 
     destination.adventures.push(adventure) 
     destination.save(function(err, advresult) { 
      console.log('push worked') 
      res.json(advresult); 
     }) 
    }) 
} 

Quando prendo l'avventura fuori dalla destination.adventures.push() il codice non si rompe, ma quando ho Inserisci avventure Ottengo un errore

/Travellog/node_modules/mongoose/lib/types/array.js:117 
    return this._schema.caster.cast(value, this._parent, false); 
          ^TypeError: undefined is not a function 
    at Array.MongooseArray.mixin._cast (/Travellog/node_modules/mongoose/lib/types/array.js:117:32) 
    at Array.MongooseArray.mixin._mapCast (/Travellog/node_modules/mongoose/lib/types/array.js:286:17) 
    at Object.map (native) 
    at Array.MongooseArray.mixin.push (/Travellog/node_modules/mongoose/lib/types/array.js:299:25) 
    at Query.<anonymous> (/Travellog/server/controllers/destinations-controller.js:28:28) 
    at /Travellog/node_modules/mongoose/node_modules/kareem/index.js:177:19 
    at /Travellog/node_modules/mongoose/node_modules/kareem/index.js:109:16 
    at process._tickCallback (node.js:355:11) 

risposta

29

L'errore che si ottiene è di conseguenza di incorporare il modello Adventure invece dello schema. È necessario aggiungere il Adventure schema nella definizione dello schema di destinazione proprietà dello schema del modello del Adventure:

// this is the "destination" model for mongoose 
var mongoose = require('mongoose'); 
var AdventureSchema = require('../models/adventure').schema; /* <- access the schema via its Model.schema property */ 

var tripSchema = mongoose.Schema({ 
    name: { type: String, required: true }, 
    city: { type: String, required: true }, 
    dateStart: { type: Date, required: true }, 
    dateFinish: { type: Date, required: true }, 
    adventures: [AdventureSchema] 
}); 
+2

Impressionante! Questo ha funzionato perfettamente, grazie a te è rimasto bloccato per sempre. Stavo seguendo i documenti di Mongoose ma, a quanto sembra, questo ha veramente a che fare con node.js? – pwborodich

+2

Grazie. Anch'io ho avuto lo stesso problema. – FarheenP

+2

Grazie mille. Provato a separare lo schema in file diversi e finito inavvertitamente includendo il modello o qualcosa del genere. 5 ore dopo. –

Problemi correlati