发送简单 JSON 响应 Nodejs

Send Simple JSON Response Nodejs

我正在尝试向连接到我的 api 的客户端应用程序发送简洁且易于解析的 json 响应。以前,我发送过这种格式的回复:

{
"success": false,
"message": "All Items Fetched",
"cartItems": [
    {
        "_id": "5abca75f43b4c21ec482e96d",
        "title": "Apples",
        "price": 594,
        "quantity": 6,
        "prodId": "5aadb71792f47742d4e3749b",
        "__v": 0
    },
    {
        "_id": "5abca75f43b4c21ec482e96e",
        "title": "Red and Mixed Grapes",
        "price": 645,
        "quantity": 5,
        "prodId": "5aac5dac664a9042a44bf787",
        "__v": 0
    },
    {
        "_id": "5abca76143b4c21ec482e96f",
        "title": "AnanaB",
        "price": 445,
        "quantity": 5,
        "prodId": "5ab1c1a8044a584bdcad6e1d",
        "__v": 0
    }
 ]
}

但现在,为了简洁起见,我想以这种方式 6 * of Apples 为每个数组项发送响应。

我试过这个代码:

let cart = await Cart.find({});
console.log('Checkout Cart:\t' + cart);

let cartArr = [cart];
console.log('Cart Arr:\t' + cartArr);

for (quantity, title in cart){
    console.log( quantity + 'x of' + title);
}

但是它在 for 循环中抛出这个错误:

SyntaxError: Invalid left-hand side in for-loop
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:599:28)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (D:\Workspace\AdminBootstrap\app.js:24:14)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
[nodemon] app crashed - waiting for file changes before starting...

如何遍历此数组以获得此输出? 谢谢。

查找returns 对象数组。

购物车如下所示

var cart = [{
    "success": false,
    "message": "All Items Fetched",
    "cartItems": [{
            "_id": "5abca75f43b4c21ec482e96d",
            "title": "Apples",
            "price": 594,
            "quantity": 6,
            "prodId": "5aadb71792f47742d4e3749b",
            "__v": 0
        },
        {
            "_id": "5abca75f43b4c21ec482e96e",
            "title": "Red and Mixed Grapes",
            "price": 645,
            "quantity": 5,
            "prodId": "5aac5dac664a9042a44bf787",
            "__v": 0
        },
        {
            "_id": "5abca76143b4c21ec482e96f",
            "title": "AnanaB",
            "price": 445,
            "quantity": 5,
            "prodId": "5ab1c1a8044a584bdcad6e1d",
            "__v": 0
        }
    ]
}];

cart.forEach((data) => {
    for (var obj in data.cartItems) {
        console.log(data.cartItems[obj].quantity + ' x of ' + data.cartItems[obj].title);
    }
});

不确定这是否是您所期望的...这是您可以循环的方式...

for(qty in obj.cartItems) {
console.log(obj.cartItems[qty].quantity + ' of ' + obj.cartItems[qty].title) }

其中 *obj 是从节点获得的对象响应。

您需要在循环中使用 of 并使用大括号进行对象解构:

for (const {quantity, title} of cart) {
    console.log('%s of %s', quantity, title);
}

当然,假设 cart 包含在您的 JSON 中显示为 cartItems 的数组。