我正在關注這個 youtube 教程,
但是在我的專案中,打字稿不起作用
app.post('/new_contact', function(req, res){
var name = req.body.name;
var phone = req.body.phone;
/* doesn't work
db.insert((name:name, phone:phone, crazy:true), phone, function(err,header ){
if(err){
res.send("Error creating database " req.body.dbname);
return;
}
return res.send("contact created successfully");
})
*/
//works
db.insert((name, phone, true), phone, function(err,header ){
if(err){
res.send("Error creating database " req.body.dbname);
return;
}
return res.send("contact created successfully");
})
});
當我按照視頻中的代碼進行操作時,出現以下錯誤:
期待很快可以收到你的來信!
uj5u.com熱心網友回復:
兄弟,你搞錯了
因為db.insert
您應該提供一個物件作為引數。
代替 :
db.insert((name:name, phone:phone, crazy:true), phone, function(err,header ){
寫 :
db.insert({name:name, phone:phone, crazy:true}, phone, function(err,header ){
( 用。。。來代替 ”{” )
uj5u.com熱心網友回復:
您的代碼中有錯字。替換(
為{
。
db.insert 將物件 {} 作為引數。
db.insert({name:name, phone:phone, crazy:true}, phone, function(err,header ){
<---somecode--->
}
uj5u.com熱心網友回復:
當你與 DB 互動時,總是發送一個 Object。在這種情況下,你需要在物件中發送姓名、電話和瘋狂。
app.post('/new_contact', function(req, res){
var name = req.body.name;
var phone = req.body.phone;
db.insert(({name:name, phone:phone, crazy:true}), phone, function(err,header ){
if(err){
res.send("Error creating database " req.body.dbname);
return;
}
return res.send("contact created successfully");
})
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/468389.html
標籤:javascript 节点.js 打字稿
上一篇:如何從有條件地回傳物件的Piniagetter中解構物件屬性?
下一篇:從輸入中獲取影像