繁体   English   中英

TypeError forEach 不是 function

[英]TypeError forEach is not a function

我正在尝试 append 数组来自 javascript forEach function 对某个容器的响应。 但是收到“response.cat_products.forEach 不是函数”的错误。 我怎么了?

    return response()->json([
        'status' => true,
        'message' => 'success',
        'cat_products' => $cat_products,
        'cat_count' => $cat_count
    ]);
    if (response.status == true) {
        response.cat_products.forEach(filter); //filter is a function

    }

当从 Laravel controller 返回响应时

return response()->json([
    'status' => true,
    'message' => 'success',
    'cat_products' => $cat_products,
    'cat_count' => $cat_count
]);

然后实际数组在 response.data 键下的 javascript 端可用。

由于您在 if 语句中进行松散比较,因此将其与 response.status: 200 进行比较,而不是您在 json 响应中提供的"status" => true

if (response.status == true) {
    //response.data should contain the data sent as response from controller
    console.log(response.data); 
    
    //response.cat_products.forEach(filter); //filter is a function

    response.data.cat_products.forEach(filter);  //this should work

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM