2017-02-21 15 views
0

Я использую Mongoose и Mongoosastic в приложении Express, у меня есть схема Mongoose course, содержащая 2 ссылки trainer и course-category. Вот мое определение модели: тренер:Пустой Mongoosastic ссылку после обновления документа

let TrainerSchema = new Schema({ 
    firstname: { 
     type: String, 
     required: true, 
     es_indexed: true, 
     es_index: 'not_analyzed', 
     es_type: 'string' 
    }... 
}, { versionKey: false }) 

TrainerSchema.plugin(mongoosastic, { 
    'index': 'training-hub', 
    'type': 'trainers', 
    'host': 'localhost', 
    'port': '9200' 
}) 
let Model = mongoose.model('trainer', TrainerSchema) 
exports.Schema = TrainerSchema 

CourseCategory:

let CourseCategorySchema = new Schema({ 
    name: { 
     type: String, 
     required: true, 
     unique: true, 
     es_type: 'multi_field', 
     es_indexed: true, 
     es_fields: { 
      name: {type: 'string', index: 'analyzed'}, 
      raw: {type: 'string', index: 'not_analyzed'} 
     } 
    }... 
}, { versionKey: false }) 

CourseCategorySchema.plugin(mongoosastic, { 
    'index': 'training-hub', 
    'type': 'course-categories', 
    'host': 'localhost', 
    'port': '9200' 
}) 
exports.Schema = CourseCategorySchema 

let Model = mongoose.model('course-category', CourseCategorySchema) 

И, наконец, курс:

let CourseSchema = new Schema({ 
    title: { 
     type: String, 
     required: true, 
     es_indexed: true, 
     es_index: 'analyzed', 
     es_type: 'string' 
    }... 
    trainer: { 
     type: Schema.Types.ObjectId, 
     ref: 'trainer', 
     required: true, 
     es_indexed: true, 
     es_schema: Trainer, 
     es_select: 'firstname lastname degree nationality slug photo' 
    }, 
    courseCategory: { 
     type: Schema.Types.ObjectId, 
     ref: 'course-category', 
     required: true, 
     es_indexed: true, 
     es_schema: CourseCategory, 
     es_select: 'name' 
    } 
}, {versionKey: false}) 

CourseSchema.index({'trainer': 1, 'title': 1}, {unique: true}) 
CourseSchema.plugin(mongoosastic, { 
    'index': 'training-hub', 
    'type': 'courses', 
    'host': 'localhost', 
    'port': '9200' 
}, { 
    populate: [ 
     { 
      path: 'trainer', select: '_id firstname lastname degree nationality slug photo' 
     }, 
     { 
      path: 'courseCategory', select: '_id name' 
     } 
    ] 
}) 

Когда я сохраняю курс впервые , Я получаю индекс, проиндексированный в elasticsearch, и поле тренера, содержащее все выбранные свойства, за исключением _id и пустого документа для поля курса, и при обновлении того же курса я получаю пустой трейнер и курс.

А вот пример из данных курса я пытался добавить/обновить с помощью этого API:

{ 
    "trainer": { 
     "_id": "58ac661bac2f3e6724d9cd02", 
     "firstname": "Jemli", 
     "lastname": "Fathi", 
     "email": "[email protected]", 
     "photo": "https://dummyimage.com/256x256&text=JemliFathi", 
     "slug": "Jemli-Fathi", 
     "updatedAt": "2017-02-21T16:08:59.446Z", 
     "createdAt": "2017-02-21T16:08:59.447Z", 
     "trainingExperiences": [], 
     "experiences": [], 
     "studies": [], 
     "nationality": "" 
    }, 
    "requirements": [ 
     "Basic knowledge of Web development", 
     "Basic HTML5 skills" 
    ], 
    "audience": [ 
     "Web developers" 
    ], 
    "title": "Getting Started with Vue.js", 
    "courseCategory": { 
     "_id": "58ac667aac2f3e6724d9cd03", 
     "name": "Web Development", 
     "description": "Design and development of Websites", 
     "createdAt": "2017-02-21T16:10:34.875Z" 
    }, 
    "description": "This course will help you get started with Vue.js. It includes Vue.js basics, Consuming external resources with Vue Resource, Routing using Vue Router and store management using Vuex", 
    "format": [ 
     "Intra-entreprise", 
     "Inter-entreprise" 
    ], 
    "duration": 4, 
    "price": 222, 
    "language": "EN", 
    "scheduleUrl": "http://res.cloudinary.com/dqihnnzaj/image/upload/v1487696105/training-hub/mpal42fdrcbbhzgmco4w.png", 
    "cover": "http://res.cloudinary.com/dqihnnzaj/image/upload/v1487696109/training-hub/nvl7pqed9q864gr1ljol.png", 
    "slug": "Getting-Started-with-Vuejs--by--Jemli-Fathi" 
    } 
+0

Какую функцию вы используете для обновления документов? – Edgar

+0

@Edgar Я использую findOneAndUpdate с опцией {new: true} – jemlifathi

+0

Предоставьте заполняемый массив в опции hydrateOptions. Я думаю, это решит проблему. – Edgar

ответ

0

Проблема заключалась в том, что я забыл заполнить мои ссылки, как сказал @Edgar, я использовал

Course.findOneAndUpdate (...). заселить ('тренер'). заселить ('') courseCategory

заселить мой тренер и courceCategory ссылки и everythin g теперь работает нормально