2017-05-09 195 views
0

我正在創建一個應用程序,您在哪裏記錄鍛鍊,我在使用貓鼬時遇到了一些問題。貓鼬嵌套模式

我有兩個模式,一個用於鍛鍊,一個用於鍛鍊。當用戶添加一個新的練習時,我希望它被存儲在鍛鍊內,而且我一直在用很多方式來嘗試這個練習。

目前,該演習是保存在我的MongoDB中不同的集合(不知道這是否是做的最好的方式),我認爲它應該保存workout.exercises內的運動,但只有objectID。我該如何解決這個問題?已經看過了填充函數,但無法弄清楚如何讓它工作。

addExercises

export function addExercise(req, res) { 
    if (!req.body.exercise.title) { 
    res.status(403).end(); 
    } 

    const newExercise = new Exercise(req.body.exercise); 

    // Let's sanitize inputs 
    newExercise.title = sanitizeHtml(newExercise.title); 
    newExercise.cuid = cuid(); 
    newExercise.sets = []; 

    newExercise.save((err, saved) => { 
    if (err) res.status(500).send(err); 
    }); 

    Workout 
    .findOneAndUpdate(
     {cuid: req.body.exercise.workoutCUID}, 
     {$push: {exercises: newExercise}}, 
     {upsert: true, new: true}, 
     function (err, data) { 
     if (err) console.log(err); 
     }); 
} 

getExercises

export function getExercises(req, res) { 
    Workout.findOne({cuid: req.params.cuid}).exec((err, workout) => { 
    if (err) { 
     res.status(500).send(err); 
    } 
    console.log(workout); 
    let exercises = workout.exercises; 
    res.json({exercises}); 
    }); 
} 

鍛鍊

import mongoose from "mongoose"; 
const Schema = mongoose.Schema; 
var Exercise = require('./exercise'); 

const workoutSchema = new Schema({ 
    title:  {type: 'String', required: true}, 
    cuid:  {type: 'String', required: true}, 
    slug:  {type: 'String', required: true}, 
    userID: {type: 'String', required: true}, 
    exercises: [{ type: Schema.Types.ObjectId, ref: 'Exercise' }], 
    date:  {type: 'Date', default: Date.now, required: true}, 
}); 

export default mongoose.model('Workout', workoutSchema); 

鍛鍊

import mongoose from "mongoose"; 
const Schema = mongoose.Schema; 
var Workout = require('./workout'); 

const exerciseSchema = new Schema({ 
    title:  {type: 'String', required: true}, 
    cuid:  {type: 'String', required: true}, 
    workoutCUID: {type: 'String', required: true}, 
    sets:  {type: 'Array', "default": [], required: true} 
}); 

export default mongoose.model('Exercise', exerciseSchema); 

回答

1

根據您的Workout模式,您聲明Exercises字段的類型是[{ type: Schema.Types.ObjectId, ref: 'Exercise' }]。這意味着這個字段應該是一個Mongoose ObjectId的數組。

看來,你試圖將整個exercise對象添加到鍛鍊的exercises領域,而不是僅僅的ObjectId。嘗試修改這樣說:

const newExercise = new Exercise(req.body.exercise); 

// Let's sanitize inputs 
newExercise.title = sanitizeHtml(newExercise.title); 
newExercise.cuid = cuid(); 
newExercise.sets = []; 

newExercise.save((err, saved) => { 
    if (err) res.status(500).send(err); 
    // Nest the Workout update in here to ensure that the new exercise saved correctly before proceeding 
    Workout 
     .findOneAndUpdate(
     {cuid: req.body.exercise.workoutCUID}, 
     // push just the _id, not the whole object 
     {$push: {exercises: newExercise._id}}, 
     {upsert: true, new: true}, 
     function (err, data) { 
      if (err) console.log(err); 
     }); 

}); 

現在你有正確的的ObjectId保存在演習現場,在查詢鍛鍊.populate應該工作:

Workout.findById(id).populate("exercises").exec((err, workout) => { 
// handle error and do stuff with the workout 
}) 
+0

謝謝!這讓它工作 –

0

Workout.findById(req.params.id).populate("exercises").exec((err,workout) =>{ 
 
res.status(200).json(workout); 
 
})

應該以這種方式工作