2015-09-24 6 views
12

अनिवार्य रूप से करने के लिए बच्चे को दस्तावेज़ जोड़ना मैं सिर्फ अपनी मौजूदा MongoDB दस्तावेज़ निम्न स्कीमामौजूदा 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) 

बाकी मार्ग

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); 
     }) 
    }) 
} 

रोमांच के लिए जब मैं destination.adventures.push() कोड उल्लंघन नहीं करती है से बाहर साहसिक ले जब मैं पोस्ट करने के लिए है, लेकिन रोमांच डालें मुझे एक त्रुटि मिलती है

/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) 

उत्तर

29

आपको जो त्रुटि मिल रही है वह परिणामस्वरूप है स्कीमा के बजाय Adventure मॉडल एम्बेड करने का। आप Adventure मॉडल स्कीमा संपत्ति गंतव्य स्कीमा परिभाषा 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

बहुत बढ़िया! यह पूरी तरह से काम किया, धन्यवाद, आप हमेशा के लिए अटक गया था। मैं मोंगोस डॉक्स का पालन कर रहा था लेकिन इसके दिखने से इसे node.js के साथ वास्तव में सही करना है? – pwborodich

+2

धन्यवाद। मुझे भी एक ही मुद्दा था। – FarheenP

+2

आपको बहुत बहुत धन्यवाद। स्कीमा को अलग-अलग फाइलों में अलग करने का प्रयास किया, और मॉडल या कुछ सहित अनजाने में समाप्त हो गया। 5 घंटे बाद –

संबंधित मुद्दे