剛學node沒多久的新手
不確定問題有沒有表達清楚
還麻煩大大們替我解惑
目前在用js練習撰寫一個按讚留言的功能
前端頁面會呼叫API將資料傳進MongoDB儲存
前端有接到API的response
但用Robo3T查看MongoDB時
資料卻沒存進去,前端頁面也沒顯示出結果
API跟資料庫的連線應該是沒問題的
因為同個js檔裡面還有其他的API
其他的API都能正常執行
所以想請問如何才能將資料儲存至mongoDB裡?
下面是網頁傳送的request
執行後收到的response
但mongoDB裡卻沒有存到資料
API的程式碼:
//修改回應
router.post("/editComment",function(req,res){
    articleModel.findById(
        req.body._id
    ,function(err,data){
        data.comment.forEach(element => {
            if(element.id==req.body.id){
                var commentIndex=data.comment.indexOf(element);
                data.comment[commentIndex].message=req.body.message;
            }
        });
        data.save(function(err){
            if(err){
                res.json({"status":1,"msg":"error"});
            }else{
                res.json({"status":0,"msg":"success","comment":data.comment});
            }
        });
    });
});
前端的js:
//編輯留言
function editComment(id){
    var msg=$(`#message${id}`);
    var btnVal=$(`#edit${id}`);
    var editArea=`<input id="inp${id}" class="ml-3" type="text" autocomplete="off"/>`;
    if(btnVal.text()=="編輯"){
        msg.hide();
        msg.after(editArea);
        btnVal.text("完成");
    }else{
        var newMsg=$(`#inp${id}`).val();
        $(`#inp${id}`).remove();
        if(newMsg){
            $.post("/blog/editComment",{
                "_id":getUrlVal("_id"),
                "id":id,
                "message":newMsg
            },function(res){
                if(res.status==0){
                    history.go(0);
                }
            });
        }
        msg.show();
        btnVal.text("編輯");
    }
}
可能是沒有 commit
Make sure the connection string for MongoDB is correct, including username, password, database name, and host address. bandle
請檢查以上各點並逐一排除問題,特別是資料格式和資料庫查詢部分 Tunnel Rush。如果有其他錯誤訊息,請提供具體的錯誤內容,這樣可以更方便地進行排查。