ga-wdi-exercises/project4

Two models not communicating

Closed this issue · 15 comments

Repo: https://github.com/nrc0004/project4

Hello,

I have 2 models that are related: programs and exercises. Exercises are supposed to belong to programs, however, I dont think I've wired them correctly and I'm not sure what I'm missing. Below is the relevant code:

Models.js

const mongoose = require('./connection.js')

const Schema = mongoose.Schema
const ObjectId = Schema.ObjectId

const ProgramSchema = new mongoose.Schema({
    name: String,
    body: String,
    exercise: [{type: Schema.ObjectId, ref: "Exercise"}]
})

const ExerciseSchema = new mongoose.Schema({
    name: String,
    sets: Number,
    reps: Number
})

const BlogSchema = new mongoose.Schema ({
    title: String,
    content: String
})

const Program = mongoose.model("Program", ProgramSchema)
const Exercise = mongoose.model("Exercise", ExerciseSchema)
const Blog = mongoose.model("Blog", BlogSchema)

module.exports = {
  Program,
  Exercise,
  Blog
}

What exactly isn't working? How do you know they're not wired up correctly?

I figured it out-- I ended up nesting the exercise model in the program model and I think that solved my issue.

Actually-- sorry, I dont think I did. I'm chatting with Nyketha now and I think we are having similar issues and she had an issue open earlier. Basically, we have nested models and the data for the nested model is in the DB, the issue is that when we call the data to be rendered on the page its not showing up.

What do your schema/model definitions look like now?

So, the more I look at it, I think my issue is they way I am trying to call the information to display on the page. Here is the model info and the ProgramShow code :

const ExerciseSchema = new mongoose.Schema({
name: String,
sets: Number,
reps: Number
})

const ProgramSchema = new mongoose.Schema({
name: String,
body: String,
exercises: [ExerciseSchema]
})

============ See highlighted for where I think my issue is =========

Program Focus:

{{vm.program.name}}

Description:

{{vm.program.body}}

Exercises

**

{{exercises.name}}

**


Update Remove Program from list

exercises is an array of objects, each of those objects being a singular exercise. It's each of those individual objects that have a name property.

So you're going to need to ng-repeat through those exercises and access the name in there.

You can say exercise.name instead of program.exercises.name. The ng-repeat gives you access to that exercise value.

It should be exercise singular, not exercises plural. Right now you have the following...

<h3 data-ng-repeat="exercise in vm.exercises">
  <p>{{exercises.name}}</p>  <!-- make this singular -->
</h3>

Is this the controller associated with this view?

function showController($state, $stateParams, Program){
  this.program = Program.get({name: $stateParams.name})
  this.update = function(){
    this.program.$update({name: $stateParams.name})
  }
  this.destroy = function(){
    this.program.$delete({name: $stateParams.name}).then(function(){
      $state.go("index")
    })
  }

If so, I don't see this.exercises defined anywhere. You need to update your ng-repeat statement so that the right side of the statement references the collection of exercises. Are they stored in this.program.exercises by any chance?

Any progress here?

Hey Adrian-- that was this issue, thank you!

Excellent! Closing but feel free to re-open if you have more questions.