我正在尝试使用特定名称获取数据库值。我想把所有的书都写给作者。

这是我的代码,用于获得作者的名字并使用Like运算符(如mysql中)搜索他在BookDB中编写的所有书籍

//get books by author name
router.route('/authorName').get((req, res) => {
    let authorName = req.params.authorName; //how can i pass this value to below regex command
    BookDB.find({"firstName": /authorName/}, (err, books) => {
        if(err) throw err;
        res.status(200).send(books);
    });
});


问题:如何将authorName传递到{“ firstName”:/ authorName /}。
我写的方式没有正确设置authorName

最佳答案

我不确定猫鼬部分,但您也可以将正则表达式创建为:
新的RegExp(exp:字符串,标志:字符串)

请注意,如果使用此格式,则开始斜杠和结束斜杠没有特殊含义,它们表示字符串以斜杠开头

关于javascript - 如何在 Mongoose 中找到“按名字”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56409831/

10-10 00:20