2016-11-19 62 views
1

我試圖將數據插入到從Angular2和Node.js的傳遞JSON來的Node.js從Angular2組件

數據庫運行我的劇本我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); 
}); 

所以在此刻。我知道我正在傳球。我知道我有一個連接到數據庫。我可以通過手動輸入並將其輸出到express route來檢索條目並將其輸出到瀏覽器。

我只是錯過了從angular2node來回傳遞數據。然後從node回到angular2

現在用echonax答案的幫助下,我有這個在網絡選項卡下的查詢字符串,

查詢字符串PARAMATERS

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

你是如何調用'addressSubmit()'? – echonax

+1

您可以在開發人員控制檯中檢查網絡請求嗎?如果你使用chrome,你可以通過打開開發工具然後點擊網絡來做到這一點。點擊觸發發佈請求的按鈕。檢查你的開發工具,看看這個調用是否被觸發,它有正確的主體和參數。然後檢查響應。你期待函數如何處理響應數據?你將響應映射到json,但你不要從函數返回json數組。你怎麼知道它不起作用? –

+0

我試圖將數據插入到數據庫中。所以在我提交表單之後,我正在檢查控制檯,並看到在那裏輸出json。然後我檢查數據庫並且還沒有創建新的條目。至於addressSubmit被稱爲是,那是運行console.log(this.address)的函數;當我檢查網絡標籤,然後點擊標題時,json不在那裏。 – wuno

回答

2

我想你是不是讓你的後端的請求。

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

線返回可觀察到的,只有當它被訂購此觀察到被調用。

這樣會讓你的addressSubmit()回報這個觀察到的是這樣的:

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); 
}) 
+0

謝謝你。當我在網絡中檢查標題時,我可以看到帶有JSON的請求。但它不會進入數據庫。我很感激你幫助我這麼多,但你會介意看我如何處理來自節點的數據嗎?另外this.addressSubmit()你打算把它放在哪裏?我注意到你添加了它,所以我想知道你是否特意說你會添加到另一個函數?我只是想了解你的邏輯,所以我不再遇到這種類型的問題。 – wuno

+0

@wuno我假設你將在angular2中的一個生命週期鉤子中使用'this.addressSubmit()'。像'ngOnInit','ngAfterViewIinit'等。因爲這是一個功能塊。你用'this'來調用它,它指的是你的組件。當我看着你的節點代碼時,我看到了一件可能會導致麻煩的小事情。在你的app.js中,console.log是你的res和req,通常數據存儲在'req.body'裏面。此外,您正在angular2端使用'this.http.post',但期望節點端的'app.get'。它不應該是'app.post'嗎? – echonax

+0

是的,我昨晚修好了。這只是事實,我沒有在後端使用帖子。非常感謝你的幫助。我不知道爲什麼我的問題被投下來。典型。 – wuno