express.js mongdb its showing data for the wrong call

I am trying to get data from data base for a certain creator or user, but it shows data even if the creator doesn’t exists.
this is the code

app.use('/api/places', placesRoutes);
router.get('/user/:uid', placesControllers.getPlacesByUserId);
const getPlacesByUserId = async(req, res, next) => {
        console.log("request data!", req.params.uid);
        const userId = req.params.uid;
        let places;
        try {
            places = await Place.find({ creater: userId });
        } catch (err) {
            const error = new HttpError('Something went wrong, could not find a place for the given user.',500);
            return next(error); 
        }
        if(!places || places.length === 0){
            return next(new HttpError('Could not find a place for the provided userid.',404));
        }
        res.json({ places: places.map( place => place.toObject({ getters: true }) ) });
    };

this the data entry saved in mondo db

{"_id":{"$oid":"62ab10baa6f33b1c588dfb8e"},"title":"ifel tower","description":"big tower","image":"https://pixabay.com/images/search/nature/","address":"Circular Rd, Walled City of Lahore, Lahore, Punjab 54000, Pakistan","location":{"lat":{"$numberDouble":"31.5924979"},"lng":{"$numberDouble":"74.3073198"}},"creator":"u1","__v":{"$numberInt":"0"}}

it should only show data on this url

api/places/user/u1

but it show the same data on different creator id’s
data with different url

>Solution :

I think it’s related to the typo in the following line:

places = await Place.find({ creater: userId });

I guess creater should be creator instead.

Leave a Reply