0

我上次挣扎如何在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)
4

1 回答 1

1

您需要为填充方法提供查询条件
像这样的东西:

const userWithCompanies = await User
  .findById(userId)
  .populate({
    path: 'companies',
    match: {
      title: {$regex: `.*${search}.*`, $options: "i"}
  });
于 2021-03-05T14:50:59.460 回答