Node.js 修改Get方法以使用Name获取数据
我有这样的问题。我正在使用MongoDB和mongoose创建一个Nodejs应用程序。我的数据库中有一个集合作为雇员。该集合的对象具有一些属性,例如id、名称、职位、办公室、薪水 我已经创建了一个节点函数,使用如下id获取员工Node.js 修改Get方法以使用Name获取数据,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有这样的问题。我正在使用MongoDB和mongoose创建一个Nodejs应用程序。我的数据库中有一个集合作为雇员。该集合的对象具有一些属性,例如id、名称、职位、办公室、薪水 我已经创建了一个节点函数,使用如下id获取员工 router.get('/:id',function (req, res) { if(!ObjectId.isValid(req.params.id)){ return res.status(400).send('No record with g
router.get('/:id',function (req, res) {
if(!ObjectId.isValid(req.params.id)){
return res.status(400).send('No record with given id :$(req.params.id)');
}
Employee.findById(req.params.id, function (err, doc) {
if(!err){
res.send(doc);
}
else{
console.log('Error in Retriving Employee:'+JSON.stringify(err, undefined, 2));
}
});
});
router.get('employee/:name'),function (req, res) {
Employee.find(req.params.name, function (err, doc) {
if(!err){
res.send(doc);
}
else{
console.log('Error in Retriving Employee:'+JSON.stringify(err, undefined, 2));
}
});
Cannot GET /employees/employee/Tharindu
Refused to load the font '<URL>' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback.
我想修改此函数以查找使用此名称的员工。我搜索并尝试了很多例子,但我找不到合适的方法来做
我试过的一种方法就是这样
router.get('/:id',function (req, res) {
if(!ObjectId.isValid(req.params.id)){
return res.status(400).send('No record with given id :$(req.params.id)');
}
Employee.findById(req.params.id, function (err, doc) {
if(!err){
res.send(doc);
}
else{
console.log('Error in Retriving Employee:'+JSON.stringify(err, undefined, 2));
}
});
});
router.get('employee/:name'),function (req, res) {
Employee.find(req.params.name, function (err, doc) {
if(!err){
res.send(doc);
}
else{
console.log('Error in Retriving Employee:'+JSON.stringify(err, undefined, 2));
}
});
Cannot GET /employees/employee/Tharindu
Refused to load the font '<URL>' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback.
}
但它并没有给出结果,它只是给了我一个这样的错误
router.get('/:id',function (req, res) {
if(!ObjectId.isValid(req.params.id)){
return res.status(400).send('No record with given id :$(req.params.id)');
}
Employee.findById(req.params.id, function (err, doc) {
if(!err){
res.send(doc);
}
else{
console.log('Error in Retriving Employee:'+JSON.stringify(err, undefined, 2));
}
});
});
router.get('employee/:name'),function (req, res) {
Employee.find(req.params.name, function (err, doc) {
if(!err){
res.send(doc);
}
else{
console.log('Error in Retriving Employee:'+JSON.stringify(err, undefined, 2));
}
});
Cannot GET /employees/employee/Tharindu
Refused to load the font '<URL>' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback.
在控制台窗口中,它向我显示如下内容
router.get('/:id',function (req, res) {
if(!ObjectId.isValid(req.params.id)){
return res.status(400).send('No record with given id :$(req.params.id)');
}
Employee.findById(req.params.id, function (err, doc) {
if(!err){
res.send(doc);
}
else{
console.log('Error in Retriving Employee:'+JSON.stringify(err, undefined, 2));
}
});
});
router.get('employee/:name'),function (req, res) {
Employee.find(req.params.name, function (err, doc) {
if(!err){
res.send(doc);
}
else{
console.log('Error in Retriving Employee:'+JSON.stringify(err, undefined, 2));
}
});
Cannot GET /employees/employee/Tharindu
Refused to load the font '<URL>' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback.
有人能帮我解决这个问题吗?谢谢。我想你打电话的方式可能有问题 数据库中的集合:
Employee.find(req.params.name, function (err, doc) {
if(!err){
res.send(doc);
}
我真的认为你应该用类似的方法:
app.get('employee/:name', function (req, res){
db.open(function(err,db){
db.collection('employee', function(err, collection) {
collection.find().toArray(function(err, items) {
console.log(items);
res.send(items);
});
});
});
});
这将返回数组集合的结果。find().toArray
以及集合中的信息
此外,使用路径
employee/:name
可能不是最佳做法之一 您从代码中获得了什么输出/错误日志<代码>db.employee.find({'name':nameVariable})是一种方法,但如果我有更多信息,我会更有帮助!:)它给了我这样一个错误拒绝加载字体“”,因为它违反了以下内容安全策略指令:“default src'self”。请注意,“font-src”未明确设置,因此“default-src”用作回退
无法获取/employees/employees/Tharindu
同时更改元安全标记:
那么将名称传递给该方法的最佳方式是什么?