我試圖將數據插入到從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
來檢索條目並將其輸出到瀏覽器。
我只是錯過了從angular2
到node
來回傳遞數據。然後從node
回到angular2
。
現在用echonax答案的幫助下,我有這個在網絡選項卡下的查詢字符串,
查詢字符串PARAMATERS
street:11700 NW 36TH AVE
city:MIAMI
state:Florida
zip:33167
你是如何調用'addressSubmit()'? – echonax
您可以在開發人員控制檯中檢查網絡請求嗎?如果你使用chrome,你可以通過打開開發工具然後點擊網絡來做到這一點。點擊觸發發佈請求的按鈕。檢查你的開發工具,看看這個調用是否被觸發,它有正確的主體和參數。然後檢查響應。你期待函數如何處理響應數據?你將響應映射到json,但你不要從函數返回json數組。你怎麼知道它不起作用? –
我試圖將數據插入到數據庫中。所以在我提交表單之後,我正在檢查控制檯,並看到在那裏輸出json。然後我檢查數據庫並且還沒有創建新的條目。至於addressSubmit被稱爲是,那是運行console.log(this.address)的函數;當我檢查網絡標籤,然後點擊標題時,json不在那裏。 – wuno