Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何传递变量以获取请求_Node.js_Mongodb_Express - Fatal编程技术网

Node.js 如何传递变量以获取请求

Node.js 如何传递变量以获取请求,node.js,mongodb,express,Node.js,Mongodb,Express,我有一个调用get请求的函数: confirmUser(username: string) { this.http.get<{ users: User[] }>('http://localhost:3000/users').subscribe( // success function (response) => { this.user = response.users; console.log(this.user); } ), (error: any) =&

我有一个调用get请求的函数:

confirmUser(username: string) {
this.http.get<{ users: User[] }>('http://localhost:3000/users').subscribe(
  // success function
(response) => {
  this.user = response.users;
  console.log(this.user);
  }
),
  (error: any) => {
    console.log(error);
  } 
 }

username的值来自表单字段,因此调用它的组件不会订阅任何URL参数,如果这会造成差异。

如果将其作为参数传递,则应使用
req.params
,否则
req.query
进行查询字符串

使用参数(即:
http://localhost:3000/users/grok
):

使用查询字符串(即:):


非常感谢你。我今天盯着这个看太久了。
router.get('/', (req, res, next) => {
// User.find().then(users => {
    User.findOne({username: "Grok"}, {} ).then(users => {
    res.status(200).json({
        users: users
    });
})
.catch(error => {
    returnError(res, error);
});
});
router.get('/:username', (req, res, next) => {
// User.find().then(users => {
    User.findOne({username: req.params.username }, {} ).then(users => {
    res.status(200).json({
        users: users
    });
})
.catch(error => {
    returnError(res, error);
});
});
router.get('/', (req, res, next) => {
// User.find().then(users => {
    User.findOne({username: req.query.username }, {} ).then(users => {
    res.status(200).json({
        users: users
    });
})
.catch(error => {
    returnError(res, error);
});
});