Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading