我挣扎在上次如何把两个查找参数在nodejs:这是我当前的查询:
userWithCompanies = await User.findById(userId).populate('companies');
但是接下来我需要让搜索系统在这里的其他部分工作搜索查询是例如:
companies = await Company.find({title: {$regex: `.*${search}.*`, $options: "i"}});
所以我试着这样做:
await User.findById(userId).populate('companies').find({title: {$regex: `.*${search}.*`, $options: "i"}});
但我只是得到错误:
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)
您需要为populate方法提供查询条件。
像这样:
const userWithCompanies = await User
.findById(userId)
.populate({
path: 'companies',
match: {
title: {$regex: `.*${search}.*`, $options: "i"}
});