iT邦幫忙

0

API測試成功 但資料無法存進MongoDB

  • 分享至 

  • twitterImage

剛學node沒多久的新手
不確定問題有沒有表達清楚
還麻煩大大們替我解惑


目前在用js練習撰寫一個按讚留言的功能
前端頁面會呼叫API將資料傳進MongoDB儲存
前端有接到API的response
但用Robo3T查看MongoDB時
資料卻沒存進去,前端頁面也沒顯示出結果
API跟資料庫的連線應該是沒問題的
因為同個js檔裡面還有其他的API
其他的API都能正常執行
所以想請問如何才能將資料儲存至mongoDB裡?

下面是網頁傳送的request
https://ithelp.ithome.com.tw/upload/images/20200819/20129589C14yBlwYXF.png

執行後收到的response
https://ithelp.ithome.com.tw/upload/images/20200819/20129589jwU9lOWXyY.png
但mongoDB裡卻沒有存到資料
https://ithelp.ithome.com.tw/upload/images/20200819/20129589Mi2qpgiBuc.png

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("編輯");
    }
}
看更多先前的討論...收起先前的討論...
wrxue iT邦好手 1 級 ‧ 2020-08-19 09:39:37 檢舉
像這篇文章內容一樣存不進嗎
好大的空白!
就白白的不知道你想問啥?
樓上兩位大大抱歉,第一次發文,不太會用
還沒打文章內容就按到送出了
找很久才找到編輯文章的地方
後來有事耽擱,所以到下午才編輯完問題
((Sorry剛用完新手任務才能留言回應大大
fillano iT邦超人 1 級 ‧ 2020-08-19 17:21:36 檢舉
後端程式先加上一些log追蹤看看程式是否有跑到自己預期的地方
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

3
海綿寶寶
iT邦大神 1 級 ‧ 2020-08-19 09:51:17

可能是沒有 commit

看更多先前的回應...收起先前的回應...
archer9080 iT邦研究生 4 級 ‧ 2020-08-19 11:14:30 檢舉

海綿大神我想知道下期威力彩號碼/images/emoticon/emoticon07.gif
拜託 求求泥/images/emoticon/emoticon02.gif

不懂/images/emoticon/emoticon06.gif
可以請海綿大說詳細一點嗎?

這題我不會
請忽略我的回答

好的,謝謝海綿大大

我要發表回答

立即登入回答