2017-12-31 162 views
0

Ich benutze MongoDB für die Persistenz Bedürfnisse meiner erholsamen API.Persistent Mungo Modell mit Array von ObjectId Referenzen

Ich habe ein Ereignisschema mit einem exportierten Modell. Ich habe auch ein Standortschema mit einem exportierten Modell.

Das Ereignis verfügt in seinem Schema über ein Array von Speicherorten, die eine oneToMany-Beziehung darstellen.

Ich mache eine HTTP-POST-Anfrage an meine API ein Event mit einer Liste der zugehörigen Standorte bestehen bleiben wie folgt:

{ 
"name": "My first advertised event", 
"shortDescription": "Some kind of event", 
"fullDescription": "This is a massive detailed description of an event, honestly.", 
"location": [ 
    "5a49121b4f1e572d48785ddd", 
    "2e21b4f1e572d48785dcb" 
    ] 

}

ich folgende Fehlermeldung erhalten, was ich kann; t scheinen um eine Lösung zu finden:

:

 "_message": "Event validation failed", 
    "message": "Event validation failed: location: Cast to Array failed for value \"[ '5a49121b4f1e572d48785ddd', '2e21b4f1e572d48785dcb' ]\" at path \"location\"", 
    "name": "ValidationError" 

Meine relevanten Schemata und Service sind wie folgt zu finden

const mongoose = require('mongoose'); 
 
const Location = require('./location'); 
 

 
const EventSchema = new mongoose.Schema({ 
 
    name: String, 
 
    shortDescription: String, 
 
    fullDescription: String, 
 
    location : [ 
 
     { type: mongoose.Schema.ObjectId, 
 
      ref: 'Location' } 
 
     ] 
 
}, { 
 
    timestamps: true 
 
}); 
 

 
mongoose.model('Event', EventSchema); 
 

 
module.exports = mongoose.model('Event');

const mongoose = require('mongoose'); 
 
const GeoLocation = require('../common/mongoose/model/geoLocation').geoLocation(); 
 

 
const LocationSchema = new mongoose.Schema({ 
 
    name: String, 
 
    description: String, 
 
    location : GeoLocation 
 
}, { 
 
    timestamps: true 
 
}); 
 

 
mongoose.model('Location', LocationSchema); 
 

 
module.exports = mongoose.model('Location');

const Hal = require('hal'); 
const Event = require('./event.js'); 

module.exports = { 

createNewEvent: (req, res) => { 
    let locations = []; 
    req.body.location.map(loc => locations.push(loc)); 
    var event = new Event({ 
     name : req.body.name, 
     shortDescription : req.body.shortDescription, 
     fullDescription : req.body.fullDescription, 
     location : req.body.location 
    }); 
    event.save() 
     .catch(err => res.status(500).send({message: err})) 
     .then(data => res.send(data) 
     ); 
    }, 
} 

Ich habe versucht, dieses Problem für Stunden zu lösen scheinen aber es kann nicht zu arbeiten. Wenn ich mit einem einzigen Verweis im JSON speichere, funktioniert es, auch wenn das Event-Schema eine oneToMany-Beziehung erwartet.

Ich bin ziemlich neu zu mongoDB, also akzeptiere ich, dass ich wahrscheinlich ein grundlegendes Konzept vermisse. Wenn jemand mir helfen kann zu lernen, was ich falsch mache, wäre ich wirklich dankbar.

Auch, guten Rutsch ins neue Jahr! :)

Antwort

0

Ich brauchte meinen Service-Aufruf zu ändern, um:

createNewEvent: (req, res) => { 
    var event = new Event({ 
     name : req.body.name, 
     shortDescription : req.body.shortDescription, 
     fullDescription : req.body.fullDescription, 
    }); 
    req.body.location.map(x => event.location.push(x)); 
    event.save() 
     .catch(err => res.status(500).send({message: err})) 
     .then(data => res.send(data) 
     ); 
} 
Verwandte Themen