2017-12-31 365 views
0

내 편안한 API의 지속성 요구 사항에 mongoDB를 사용하고 있습니다.ObjectId 참조 배열이있는 머스트 몽구스 모델

내 보낸 모델이있는 이벤트 스키마가 있습니다. 내 보낸 모델이있는 위치 스키마도 있습니다.

이벤트의 스키마에 oneToMany 관계를 나타내는 Locations의 배열이 있습니다. 다음과 같이

나는 관련 위치 목록과 함께 이벤트를 계속 내 API에 HTTP POST 요청을합니다

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

}

나는 내가 할 수있는 다음과 같은 오류 얻을; t 보일를 에 대한 해결책 찾기 :

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

내 관련 스키마와 서비스를 아래에서 찾을 수 있습니다

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

나는 시간이 문제를 해결하기 위해 노력했지만 그것을 밖으로 작동하지 수

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');
. JSON에 단일 참조를 저장하면 이벤트 스키마가 oneToMany 관계를 기대한다고 생각합니다.

임 mongoDB에 새로운 것이므로 기본 개념이 누락되었습니다. 아무도 내가 무엇을 잘못하고 있는지 알 수 없다면 정말 감사 할 것입니다.

또한 새해 복 많이받습니다!

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

답변

0

나는 내 서비스 요청을 개정 할 필요