【问题标题】:Node PUT request loop error节点 PUT 请求循环错误
【发布时间】:2017-11-29 20:26:48
【问题描述】:

我昨晚问了一个类似的问题,几乎没有得到任何意见和答复。所以我再次尝试我的运气,因为我无法解决这个问题并在过去两天继续我的项目!

这是昨天问题的链接:Mongoose - No matching document found (ForEach)

基本上,我的客户端上有一个表,其中有一个行 ID 和用于更新和删除行的按钮。单击更新按钮后,它将行 id 发送到服务器并执行 PUT 请求,这非常有效。问题来自我单击的另一行,它使用相同的新数据更新前一行和新单击的行。

我已经上传了一个 youtube 视频来解释这个问题,以防我在这里没有很好地解释它:https://www.youtube.com/watch?v=G8sAP3AONYI

这是我的代码:(我已将我的 github 链接添加到项目中):

User.js(模型)

local: {
    ...,
    education: [{
        id: String,
        degree: String,
        institute: String,
        dates: String,
        description: String
    }],
    ...
}

router.js

app.put('/put_education/:id', function(req, res) {
    var row_id = req.params.id; //get education id from table row

    //get the current logged in user
    User.findById(req.user._id, function (err, doc) {
        if (err) {
            console.log('no entry found');
        }

        //match the table row id with the id in users education model
        doc.local.education.forEach(function (education, index) {
            console.log(education.id + "   " + row_id);

            //if rows match, replace the database document with values from client
            if(education.id === row_id){
                doc.local.education[index] = req.body;
                doc.save(function (err) {
                    if (err) {
                        console.log(err);
                    } else {
                        res.send("Success");
                    }
                });
            }
        });
    });
});

客户端 javascript

//when edit button on row clicked, change 'save' button to 'update' inside the model
$('#education_body').on('click', '.edit_education', function() {
    var row_id = get_row_id(this);

    $("#education_save_button").hide();
    $("#education_update_button").show();

    //when update button clicked inside the model, call PUT in server
    $('#education_form').on('click', '#education_update_button', function (event) {
        event.preventDefault();
        $.ajax({
            url: '/put_education/' + row_id,
            method: 'PUT',
            contentType: 'application/json',
            data: JSON.stringify({
                id: $('input[name="education_id"]').val(),
                degree: $('input[name="education_degree"]').val(),
                institute: $('input[name="education_institute"]').val(),
                dates: $('input[name="education_dates"]').val(),
                description: $('textarea[name="education_description"]').val()
            }),
            success: function (response) {
                initialize(); //function to fill in all textfields values from db
                $("#education_saved").show().delay(3000).fadeOut();
            }
        });

        //change 'update' button back to 'save'
        $("#education_save_button").show();
        $("#education_update_button").hide();
    });
});

请参考我之前的问题以查看错误的图像以及究竟发生了什么..

我想在 id 匹配后中断循环,但 foreach 循环没有中断功能,我将其更改为正常的 for 循环,但它仍然给我同样的错误。所以我不认为打破循环是一个答案..

Github:https://github.coventry.ac.uk/salmanfazal01/304CEM-Back-End

提前致谢:)

【问题讨论】:

  • 我认为您的问题来自客户端。你检查过你发送的 id 正确吗?
  • 是的,它确实发送了正确的 ID。 100%。每次单击编辑按钮时,我都会添加一个 console.log,它会打印正确的行,一次打印一个。
  • 我认为你的提交事件发生了两次我看到你的视频。当您第二次单击编辑时发送 2 个请求而不是 1 个。您需要检查客户端代码为什么要触发 event2 次,也许您每次单击一行时都添加提交事件?
  • @AmitWagner 我不确定,但我在 ajax 函数中添加了一个 print 并且成功返回,它们都只在每次更新点击时发生一次。我绝对确定错误来自服务器端,在循环内部,因为它循环不止一次 - 前一行点击了新行。
  • 你能不能在开头放一个控制台日志,看看你有多少次得到这条路线?

标签: javascript node.js mongodb for-loop foreach


【解决方案1】:

我通过将内部 click() 函数与外部函数分开来解决它。不完全确定为什么我的初始代码不起作用,但这解决了它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    相关资源
    最近更新 更多