Express For route.patch中的循环无效。|Exprees.js

Express For route.patch中的循环无效。|Exprees.js,express,Express,下面是我为/:productId定义的route.patch函数的一段代码 route.patch('/:productId',(req,res,next)=>{ const id = req.params.productId; const updateOps = {}; console.log(req.body); for (const ops of req.body) { updateOps[ops.propName] = op

下面是我为/:productId定义的route.patch函数的一段代码

route.patch('/:productId',(req,res,next)=>{
    
    const id = req.params.productId;
    const updateOps = {};
    console.log(req.body);
    for (const ops of req.body) {
        updateOps[ops.propName] = ops.value;
        console.log(updateOps);
    }
    
    res.status(200).json({Message: 'Hi'});
});
for循环之前的console.log(req.body)正在工作,但是for循环内部的console.log(updateOps)不工作。甚至还有res.status(200.json)({Message:'Hi'});它不起作用。我得到一个找不到的路由错误。 基本上,for循环之前的一切都在工作,而for循环之后的一切都不工作

你能告诉我哪里做错了吗。这是我在route.patch中循环使用的方式吗?非常感谢您的帮助


感谢您回答评论中有关错误处理的问题。您有以下中间软件:

app.use((req, res, next) => {
    const error = new Error('Not found');
    //console.log(error.status) //console.log(error.message) 
    error.status = 404; //console.log(error.message) 
    next(error);
});

app.use((error, req, res, next) => {
    res.status(error.status || 500);
    res.json({
        error: {
            message: 'Route not found'
        }
    })
});
因此,对于每个请求,都将创建一个错误对象,您将其分配给状态
404
,并将其传递给下一个中间件。您将在那里发送错误响应,由于状态已设置为
404
,因此使用此状态


实际上,您应该删除创建错误的第一个中间件,因为这将针对每个请求执行。Express已处理对不存在的路由的请求,并将返回
404
notfound响应。您可以保留第二个中间件来处理任何其他未处理的错误(您应该更改错误消息,尽管:)

是否
req.body
包含一个iterable,例如数组?感谢eol,到目前为止,我一直在body中传递一个Json对象。我忘了我设计它是为了通过一个数组。现在我的问题在通过一个数组后就解决了。但是,您能解释一下,当我传递一个Json对象时,为什么找不到它的显示路径吗?顺便说一句,我正在捕获路由中的错误。您可以添加捕获错误的代码吗?//错误处理应用程序。使用((req,res,next)=>{const error=new error('Not found');//console.log(error.status)//console.log(error.message)error.status=404;//console.log(error.message)next(error);});app.use((error,req,res,next)=>{res.status(error.status | | | 500);res.json({error:{message:'Route not found'}});非常感谢你的回答和建议。你太棒了!!:)没问题-请点击左边的复选标记接受答案,谢谢!