Closed jawknee59 closed 1 year ago
Share the code for your models in separate code blocks in a reply here.
favorite model
/////////////////////////////////////////////////////////
//// Schema for the review subdocument ////
/////////////////////////////////////////////////////////
const mongoose = require('./connection')
// destructure the schema function from mongoose
const { Schema } = mongoose
const favoriteSchema = new Schema ({
owner: {
type: Schema.Types.ObjectId,
ref: 'User'
},
banhmi: {
type: Schema.Types.ObjectId,
ref: 'Banhmi'
}
})
////////////////////////////////////
//// Export Schema ////
////////////////////////////////////
module.exports = favoriteSchema
User model:
// import what I need
const { Schema, model } = require('./connection.js')
const favoriteSchema = require('./favorite.js')
// create the schema
const UserSchema = new Schema(
{
username: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
favorites: [favoriteSchema]
},
{ timestamps: true }
)
// creat the model
const User = model('User', UserSchema)
// export the model
module.exports = User
favorites should be their own model, so add the line that makes them a model and then use mongoose model queries(create, findById, etc.) tied to their own controllers. There's not a reason to have an owner
on the favorite and to also push them into an array for the user. Operate with your favorites being a model, as this is best practices for a data structure like this one.
Thank you, I'll try that and get back to you!
I am confused about how I retrieve the banhmi's info and add it to my favorites page.
nvm i think i figured it out
What's the problem you're trying to solve?
Trying to understand how to grab an existing model and attach it to the user's favorites array, would like to have someone to talk to to go through what I am trying to execute.
Post any code you think might be relevant (one fenced block per file)
If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?
What is your best guess as to the source of the problem?
I want to press the 'favorite' btn and post the model's information to the favorites page.
What things have you already tried to solve the problem?
Paste a link to your repository here
https://github.com/jawknee59/project2-banh-mi