Express script not working

One of my mongoDB calls is not functioning, and I am unable to figure out why. All others work properly.

The following code is the call

router.get("/", (req, res, next) => {
    console.log("BEGIN")
    users.find(
        {
            _id: req.query._id
        },
        {
            perms: 1,
            lastName: 1
        },
        (error, data) => {
            if(error) {
                return next(error);
            } else {
                console.log("data: "+data)
                res.json(data);
            }
            console.log("END")
        }
    )
})

This is the output of the log:

BEGIN
data: 
END
GET /userData/?_id=62c5ca2734ed51f3e82bbe56 200 50.509 ms - 2

No data is sent. “perms” is an object and lastName is a string.

Just a wild guess since you don’t show enough to make certain, but quite possibly req.query._id is null or nonsense.

The ID is used elsewhere (login), and matches the format in the database

The ID is used elsewhere (login), and matches the format in the database

Quite possibly so. To make sure, why not console.log() it right there with the BEGIN.

when querying a proper _id field generated by MongoDB during inserts (or with proper id function), you need to use the same method to fetch it back.

In javascript, you would also need to import the function.

import { ObjectId } from "bson"
....
users.find( { _id: ObjectId( req.query._id ) } )
....
1 Like