【发布时间】:2021-03-07 19:59:53
【问题描述】:
我将 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
});
从视图中,我发送了一个格式为 /vaccine/{vaccine.name} 的 URL,其中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');
};
它给出了这个错误,说我不能使用'。'在查询中:
Company.findOne({vaccine.name: req_url}, function(err, user){
^
SyntaxError: Unexpected token '.'
使用给定的vaccine.name 获取用户的替代方法是什么?
【问题讨论】:
标签: node.js mongodb express mongoose model-view-controller