Home > database >  MongoDB: Find by ObjectID retrieves whole record although not matching numbers
MongoDB: Find by ObjectID retrieves whole record although not matching numbers

Time:01-22

I am using the following function to return all records, which match the ObjectIDs whereas "user_id" is virtually the foreign key.

The problem: The whole record is always retrieved, although the ObjectIDs do not match in the database.

That means "user_id" from Customer does not equal "req.user.id" from User.

What is the issue here?

// Get All Customers By User ID

app.get('/api/users/get_customers', requireAuth, async (req, res) => {
    console.log("User: "   req.user.id   " attempted to load customers.")
    const customers = await Customer.find({
        user_id: mongoose.Types.ObjectId(req.user.id)
    });
    res.status(200).json(customers);
    console.log(customers);
});

CodePudding user response:

You should use req.user._id instead req.user.id.

CodePudding user response:

I solved the issue. I used a wrong schema and updated it. Not it is working.

  •  Tags:  
  • Related