Add data after mongodb node.js connection

app.get("/add",function(req,res){
      // 
      //
     MongoClient.connect(shujukuURL,function(err,db){
       if(err){
            console.log(err);
            console.log("");
            return;
       }
       res.send("123")
       //
       db.collection("user").insertOne({
           "name":"node.js",
           "age":13
       },function(error,result){
         if(error){
               console.log(error);
                console.log("");
               return;
          }
          res.send("");
          db.close();//
       })
     })
});

problem: after the above code connects to the mongodb database, you can add a piece of data like the database, but the res.send () method is not executed in the browser, and the cmd command box reports an error. Have you ever encountered similar problems? please take a look, thank you.

Mar.18,2021

the problem should be that you used res.send twice, and the error prompt in cmd was made in line 49 of app3.js.


res.send ("123") just remove it here. You forgot to delete it in this test.

Menu