Reputation: 81
I struggle in last time how to put two find paraments in nodejs: This is my currently query:
userWithCompanies = await User.findById(userId).populate('companies');
But in the next i need to make search system here in other sections working search query is for example:
companies = await Company.find({title: {$regex: `.*${search}.*`, $options: "i"}});
So i tried something like this:
await User.findById(userId).populate('companies').find({title: {$regex: `.*${search}.*`, $options: "i"}});
But i just getting error:
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
Upvotes: 0
Views: 38
Reputation: 593
You need to provide query condition for populate method.
Something like this:
const userWithCompanies = await User
.findById(userId)
.populate({
path: 'companies',
match: {
title: {$regex: `.*${search}.*`, $options: "i"}
});
Upvotes: 1