无法在猫鼬中使用 Model.findbyId
Unable to use Model.findbyId in mongoose
我正在使用 MongoDB 以及 nodejs、expressjs、ejs 和 passport。我已经设置了 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
的用户有什么替代方案?
您不能在查询中使用 属性 访问器,将您的 vaccine.name
括在引号中,如 'vaccine.name'
.
我正在使用 MongoDB 以及 nodejs、expressjs、ejs 和 passport。我已经设置了 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
的用户有什么替代方案?
您不能在查询中使用 属性 访问器,将您的 vaccine.name
括在引号中,如 'vaccine.name'
.