鍍金池/ 問答/HTML/ mongodb node.js 連接后新增數(shù)據(jù)

mongodb node.js 連接后新增數(shù)據(jù)

app.get("/add",function(req,res){
      //增加數(shù)據(jù) 
      //連接數(shù)據(jù)庫
     MongoClient.connect(shujukuURL,function(err,db){
       if(err){
            console.log(err);
            console.log('數(shù)據(jù)庫連接失敗');
            return;
       }
       res.send("123")
       //增加數(shù)據(jù)
       db.collection("user").insertOne({
           "name":"node.js",
           "age":13
       },function(error,result){
         if(error){
               console.log(error);
                console.log('數(shù)據(jù)增加失敗');
               return;
          }
          res.send("增加數(shù)據(jù)成功");
          db.close();//關(guān)閉數(shù)據(jù)庫
       })
     })
});

圖片描述

問題: 上述代碼連接mongodb數(shù)據(jù)庫之后,可以做到像數(shù)據(jù)庫添加一條數(shù)據(jù),但是在瀏覽器里面卻沒有執(zhí)行res.send()方法,并且cmd 命令框報錯了,有沒有遇到過類似問題的,請各位幫忙看看,謝謝。

回答
編輯回答
尛曖昧

res.send("123")這里去掉就好了,你這個是測試忘了刪掉吧

2018年7月12日 00:55
編輯回答
疚幼

問題應(yīng)該出在你使用了2次res.send的原因,而且cmd中錯誤提示是在app3.js中的第49行出的錯。

2018年9月7日 07:08