【问题标题】:Passing Json To Node.js From Angular2 Component从 Angular2 组件将 Json 传递给 Node.js
【发布时间】:2017-04-03 13:01:44
【问题描述】:

我正在尝试从 Angular2 和 Node.js 将数据插入数据库

当我运行我的脚本时,我console.log(this.address); 以确保我传递了json,这是控制台的输出。

Address {street: "1111 NW 40TH AVE", city: "MIAMI", state: "Florida", zip: "33167"}

但是记录没有进入数据库。我知道我有联系,但我在这里遗漏了一些东西,不知道如何从这里进行故障排除。

在组件中这是http.post

 addressSubmit() { 
      this.addressSubmitted = true; 
      var headers = new Headers();
      headers.append('Content-Type', 'application/json');
      this.http
      .post('http://localhost:4200/profile/addAddress', this.address, { headers: headers })
      .map(response => response.json());
      console.log(this.address);
  }

所以有了这个函数,我知道我有一个似乎正在传递 json 的函数。

然后在带有节点的后端我收到这样的json,

addAddress: function(req, res) {
        pool.connect(function(err, client, done) {
            if (err) {
                return console.error('error fetching client from pool', err);
            } // end of error catch while creating pool connection

            var query = client.query("insert into address (street, city, state, zip) " +
            "values ('" + req.query.street + "','" + req.query.city + "','" +
            req.query.state + "','" + req.query.zip + "')"); // create the query

            query.on("end", function(result) {
                client.end();
                res.write('Success');
                res.end();
            }); // handle the query
            done(); // release the client back to the pool
        }); // end of pool connection
        pool.on('error', function(err, client) { 
                console.error('idle client error', err.message, err.stack) 
            }); // handle any error with the pool
    } // End addAddress function

像这样处理路线,

router.get('/profile/addAddress', connection.addAddress);

在 app.js 中

app.get('/profile/addAddress', function(req,res){
    db.addAddress(req,res);
});

所以在这一点上。我知道我正在传递 json。我知道我与数据库有连接。我可以通过手动输入条目并转到express route 来检索条目并将它们输出到浏览器。

我只是缺少从angular2node 来回传递数据。然后从node回到angular2

现在在 echonax 答案的帮助下,我在“网络”选项卡下的查询字符串中有这个,

查询字符串参数

street:11700 NW 36TH AVE
city:MIAMI
state:Florida
zip:33167

【问题讨论】:

  • 你怎么打电话给addressSubmit()
  • 能否在开发者控制台中查看网络请求。如果您使用 chrome,您可以通过打开开发工具然后单击网络来执行此操作。单击触发发布请求的按钮。检查您的开发工具以查看调用是否已触发并且它具有正确的正文和参数。然后检查响应。您期望该函数对响应数据做什么?您将响应映射到 json,但不从函数返回 json 数组。你怎么知道它不起作用?
  • 我正在尝试将数据插入数据库。所以在我提交表单后,我正在检查控制台,我看到那里输出了 json。然后我检查数据库并没有创建新条目。至于 addressSubmit 被称为是,这是运行 console.log(this.address); 的函数当我检查“网络”选项卡然后单击“标题”时,json 不在其中。

标签: javascript json node.js angular


【解决方案1】:

我认为您没有向后端发出请求。

你的

this.http
      .post('http://localhost:4200/profile/addAddress', this.address, { headers: headers })
      .map(response => response.json());

line 返回一个 Observable,这个 observable 只有在订阅时才会被调用。

所以让你的 addressSubmit() 像这样返回这个 observable:

 addressSubmit() { 
      this.addressSubmitted = true; 
      var headers = new Headers();
      headers.append('Content-Type', 'application/json');
      return this.http
      .post('http://localhost:4200/profile/addAddress', this.address, { headers: headers })
      .map(response => response.json());
  }

然后在你的代码中这样称呼它:

this.addressSubmit().subscribe((response)=>{
    //do something with the response here
    console.log(response);
})

【讨论】:

  • 谢谢哥们。当我现在在网络中检查标头时,我可以看到包含 JSON 的请求。但它不会进入数据库。感谢您对我的帮助,但您介意看看我如何为我处理来自节点的数据吗?还有 this.addressSubmit() 你打算把它放在哪里?我注意到您将其添加到其中,所以我想知道您是否专门说要将其添加到另一个函数中?我只是想了解您的逻辑,因此我不会再遇到此类问题。
  • @wuno 我假设您将在 angular2 的生命周期挂钩之一中使用 this.addressSubmit()。像ngOnInitngAfterViewIinit 等。因为这是一个功能块。你用this 调用它,它指的是你的组件。当我查看您的节点代码时,我看到了一个可能导致麻烦的小东西。在你的 app.js 中,console.log 你的 resreq,通常数据是通过 post 操作存储在 req.body 中的。此外,您在 angular2 端使用 this.http.post,但在节点端使用 app.get。不应该是app.post 吗?
  • 是的,我昨晚修好了。只是我没有在后端使用 post 的事实。非常感谢你的帮助。我不知道为什么我的问题被否决了。典型的。
猜你喜欢
  • 2017-05-12
  • 1970-01-01
  • 2018-07-15
  • 2023-03-30
  • 2017-10-08
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多