这里需要一些帮助,因为我对一些本应非常简单的事情感到非常迷失。要么我就失去理智了。
好吧,我正在使用express.Router()来路由我的路线
我会直接跳到问题所在:
这有效:
apiRouter.get('/api/user/:id', secureAPIPages, function(req, res, next){
userModel.findOne({'profileID':req.params.id}, function(err, user) {
if(user){
res.json({
fullname: user.fullname,
about: user.about,
birthday: user.birthday,
email: user.email,
location: user.location
});
} else {
console.log('Result does not exist');
}
});
})
所以当我在浏览器上调用电话时,例如: http://localhost:3000/api/user/123456
它获取“id”变量并将其放入 req.params 中,这样一切都很好。
现在,问题来了(这就是我想要实现的,上面的只是测试我的 API 路由是否有效):
apiRouter.get('/api/user', secureAPIPages, function(req, res, next){
userModel.findOne({'profileID':req.params.id}, function(err, user) {
if(user){
res.json({
fullname: user.fullname,
about: user.about,
birthday: user.birthday,
email: user.email,
location: user.location
});
} else {
console.log('Result does not exist');
}
});
})
当我在浏览器上运行此 URL 时,为什么它没有获取我的“id”和“name”变量: http://localhost:3000/api/user?id=123456789&name=Shayan
一如既往,提前感谢您的帮助。
沙彦
最佳答案
您应该按如下方式使用查询对象:
如果这是网址:
http://localhost:3000/api/user?id=123456789&name=Shayan
使用
req.query.id,req.query.name
关于node.js - 如何在 Node 和 Express 中从 API 调用中获取多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37024037/