Restaurant review won't get updated

Hello, I’m doing the 'MERN Stack Course - ALSO: Convert Backend to Serverless with MongoDB Realm ’ and I’m at the bit where we use the put methods to update the reviews in mongodb. For some reason, it keeps adding a new review, and it connects to a new restaurant id??

Here is my code for ReviewDao and Review Ctrl:

Review Controller:

import ReviewsDao from "../dao/reviewsDAO.js"


export default class ReviewsController {
    static async apiPostReview(req, res, next) {
        try {
            const restaurantId = req.body.restaurant_id
            const review = req.body.text
            const userInfo = {
                name: req.body.name,
                _id: req.body.user_id
            }
            const date = new Date()

            const reviewResponse = await ReviewsDao.addReview(
                restaurantId,
                userInfo,
                review,
                date,
            )
            res.json({ status: "success" })
        } catch (e) {
            res.status(500).json({ error: e.message })
        }
    }

    static async apiUpdateReview(req, res, next) {
        try {
            const reviewId = req.body.review_id
            const text = req.body.text
            const date = new Date()

            const reviewResponse = await ReviewsDao.addReview(
                reviewId,
                req.body.user_id,
                text,
                date,
            )

            var { error } = reviewResponse
            if(error) {
                res.status(400).json({ error })
            }

            if(reviewResponse.modifiedCount === 0) { // if not updated
                throw new Error(
                    "unable to update review - user may not be original poster"
                )
            }

            res.json({ status: "success" })
        } catch (e) {
            res.status(500).json({ error: e.message })
        }
    }

    static async apiDeleteReview(req, res, next) {
        try {
            const reviewId = req.query.id
            const userId = req.body.user_id
            console.log(reviewId)
            const reviewResponse = await ReviewsDao.deleteReview(
                reviewId,
                userId,
            )
            res.json({ status: "success" })
        } catch (error) {
            res.status(500).json({ error: e.message })
        }
    }
}

ReviewDAO:

import mongodb from "mongodb"
const ObjectId = mongodb.ObjectId

let reviews

export default class ReviewsDAO {
    static async injectDB(conn) {
        if (reviews) {
            return
        } 
        try {
            reviews = await conn.db(process.env.RESTREVIEWS_NS).collection("reviews")
        } catch (e) {
            console.error(`Unable to establish collection handles in userDao: ${e}`)
        }
    }

    static async addReview(restaurantId, user, review, date) {
        try {
            const reviewDoc = { 
                name: user.name,
                user_id: user._id,
                date: date,
                text: review,
                restaurant_id: ObjectId(restaurantId),
            }
            return await reviews.insertOne(reviewDoc) // insert into the database
        } catch (e) {
            console.error(`Unable to post review: ${e}`)
            return { error: e }
        }
    }

    static async updateReview(reviewId, userId, text, date) {
        try {
            const updateResponse = await reviews.updateOne(
                { user_id: userId, _id: ObjectId(reviewId) }, // looks for the review with the id
                { $set: { text: text, date: date } }, // set text
            )

            return updateResponse
        } catch (e) {
            console.error(`Unable to update review: ${e}`)
            return { error: e }
        }
    }

    static async deleteReview(reviewId, userId) {
        try {
            const deleteResponse = await reviews.deleteOne({
                _id: ObjectId(reviewId),
                user_id: userId,
            })

            return deleteResponse
        } catch (e) {
            console.error(`Unable to delete review: ${e}`)
            return { error: e }
        }
    }
}

You use addReview in apiUpdateReview instead of updateReview.

2 Likes

my god my brain cannot fathom this askljdhasdj

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.