无法使用模型.findbyId in mongoose



我正在使用MongoDB与nodejs, expressjs, ejs和护照。我已经设置了MVC,并从视图发送一个动态URL。

这是我的MongoDB模式:

const companySchema = new mongoose.Schema({
country: {
type: String,
},
username: {
type: String,
unique: true,
},
password: {
type: String,
},
vaccine: {
name: String,
price: Number,
}
}, {
timestamps: true
});

从视图中,我发送了一个URL,格式为/vaccine/{vaccine.name},其中vaccine.name是模式中的数据。在服务器端,我正在切割URL以获得确切的疫苗名称。这是我获取用户的查询:

const Company = require("../models/company");
module.exports.transaction_req = function (req, res) {
if (req.isAuthenticated()) {
const req_url = req.url.slice(1);
// console.log(req_url);
Company.findOne({vaccine.name: req_url}, function(err, user){
return res.render('transaction');
})
}
return res.redirect('/login');
};

它给出这个错误说我不能使用'。' in query:

Company.findOne({vaccine.name: req_url}, function(err, user){
^
SyntaxError: Unexpected token '.'

对于给定的vaccine.name,获得用户的替代方案是什么?

您不能在查询中使用属性访问器,将您的vaccine.name包入引号,如'vaccine.name'

最新更新