如何向此 Node JS 函数添加变量

时间:2021-07-27 14:20:06

标签: javascript node.js mongodb telegram

我正在使用 NodeJS 和 MongoDB 创建电影搜索机器人。我使用以下代码搜索电影。

int missingThings = 0;
if (row1.thing1 && row2.thing1) missingThings++;
if (row1.thing2 && row2.thing2) missingThings++;

以上代码为我提供了完美的搜索结果。 但我不知道如何将 word1word2 参数作为变量插入到函数中。如果我用参数替换 avengers2012 单词,它会读取为常量。 如何插入它们以用作变量...

2 个答案:

答案 0 :(得分:0)

您需要使用 Javascript Template string

$search: `"${word1}" "${word2}"`

答案 1 :(得分:0)

你可以使用

const query = {
    $text: {
        $search: `\"${word1}\" \"${word2}\"`
    }
}