[英]How can I pass a query param from my service on the front end to my server?
I'm trying to get the user data back from my database, but I can't seem to figure out how to send a query param to my server in order to do such a thing.我正在尝试从我的数据库中取回用户数据,但我似乎无法弄清楚如何将查询参数发送到我的服务器以执行此操作。 My angular front end is running on a different port than my server (not sure that matters) but I can't figure it out.
我的 angular 前端运行在与我的服务器不同的端口上(不确定是否重要),但我无法弄清楚。
I've tried using http.get to call the backend, but that doesn't seem to work.我试过使用 http.get 来调用后端,但这似乎不起作用。
This is the code in my auth.service:这是我的 auth.service 中的代码:
reload() {
this.http.get('/user', {params: {userId: 'test'}});
}
This is the code on my server这是我服务器上的代码
app.get("/user/?userId", (req, res, next) => {
console.log('hello 2ddsaczczxczxC')
console.log(req);
})
My console logs aren't logging anything so it's clearly not reaching the backend at all.我的控制台日志没有记录任何内容,因此它显然根本没有到达后端。
Here is the change required for accessing any as such request parameters.这是访问任何此类请求参数所需的更改。 Update your rest end point to
将您的 rest 端点更新为
/user/:userid
app.get("/user/:userId", (req, res, next) => {
console.log('hello 2ddsaczczxczxC')
console.log(req.params);
})
Then access the query using either by request.params or req.query please check the documentation of expressjs as well.然后使用 request.params 或 req.query 访问查询,请同时查看 expressjs 的文档。
You can try this:你可以试试这个:
app.get("/user/:userId", (req, res, next) => {
console.log();
}
You can pass query parameter by doing this您可以通过这样做传递查询参数
reload() {
this.http.get('/user', {queryParams: {userId: 'test'}});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.