From 626c96d9644b25e2976c97a011d3ca67edb122a9 Mon Sep 17 00:00:00 2001 From: Prajjwal Nijhara Date: Fri, 12 Jun 2020 20:31:20 +0530 Subject: [PATCH] model: FoodUp and user association --- models/foodup.js | 21 ++++++++++++++------- routes/FoodUp.js | 7 ++++++- views/food-up/show.ejs | 4 ++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/models/foodup.js b/models/foodup.js index 39a2496..a4c78f9 100644 --- a/models/foodup.js +++ b/models/foodup.js @@ -3,13 +3,20 @@ const mongoose = require("mongoose") const foodDataSchema = new mongoose.Schema({ name: String, description: String, - image: String, - comments: [ - { - type: mongoose.Schema.Types.ObjectId, - ref: "Comment" - } - ] + image: String, + author: { + id: { + type: mongoose.Schema.Types.ObjectId, + ref: "User" + }, + username: String + }, + comments: [ + { + type: mongoose.Schema.Types.ObjectId, + ref: "Comment" + } + ] }) const foodData = mongoose.model("foodData", foodDataSchema) diff --git a/routes/FoodUp.js b/routes/FoodUp.js index f71dc3f..051b6c5 100644 --- a/routes/FoodUp.js +++ b/routes/FoodUp.js @@ -22,10 +22,15 @@ router.post("/FoodUp", isLoggedIn, function(req, res){ const name = req.body.name const description = req.body.description const image = req.body.image + const author = { + id: req.user._id, + username: req.user.username + } const newfoodData = { name: name, description: description, - image: image + image: image, + author: author } // foodData.push(newfoodData) foodData.create(newfoodData, function(err, newfoodData){ diff --git a/views/food-up/show.ejs b/views/food-up/show.ejs index ce0ad29..67652e1 100644 --- a/views/food-up/show.ejs +++ b/views/food-up/show.ejs @@ -19,6 +19,10 @@

<%= foundFoodData.name %>


<%= foundFoodData.description%>

+
+

+ Submitted by <%= foundFoodData.author.username %> +