【问题标题】:Unable to target a nested object in JSON by using dot notation无法使用点表示法定位 JSON 中的嵌套对象
【发布时间】:2020-08-25 04:31:43
【问题描述】:

我正在使用 mongoose 和 express 访问 MongoDB 中的数据,当我引用数据库时可以看到 JSON,但是当我尝试定位嵌套在其中的对象时,我收到一条未定义的消息。

用于访问数据库的 JS 文件。

const express = require('express');
const router = express.Router();
const skate_tricks = require('../models/SkateTrick');

router.get('/', (req, res) => {
    skate_tricks.find({}, (err, result) => {
        if (err) {
            res.send(err);
        } else {
            res.send(result);
        }
    });
});

module.exports = router;

使用 Postman 的结果 sn-p

[
    {
        "trick_type": [],
        "_id": "5f34a5782e0d2fe1c2847633",
        "tricks": [
            {
                "trick_id": 0,
                "trick_name": "ollie",
                "trick_type": [
                    "flat"
                ],
                "trick_difficulty": "n",
                "trick_description": "Fundamental trick involving jumping with the skateboard. Performed by popping the tail with the back foot and sliding the front foot up towards the nose of the board. The sliding action levels the board while you are airborn."
            }
        ]
    }
]

我想直接访问对象的“tricks”数组,但它不起作用。

res.send(result.tricks);

【问题讨论】:

  • 看来result是一个数组,需要使用result[0].tricks来获取值。
  • 您的结果变量是一个数组,因此您需要像这样访问它result[0].tricks
  • @HaoWu 当我尝试用你的方法控制台注销它时,我仍然不确定。我也注意到了,但不确定为什么它不起作用。
  • 也许你可以试试console.log(JSON.parse(JSON.stringify(result))) 看看里面到底有什么
  • @HaoWu 我在控制台中得到了相同的结果。它表明数据被包裹在一个带有方括号的数组中。我以前使用过 JSON,但从未遇到过这个问题。

标签: javascript node.js json mongodb express


【解决方案1】:

尝试将tricks 添加到您的猫鼬模型中

const mongoose = require("mongoose");
const SkateTrickSchema = new mongoose.Schema({
  trick_id: { type: Number, required: true, unique: true },
  trick_name: { type: String, unique: true },
  trick_type: { type: Array, required: true },
  trick_difficulty: { type: String, required: false },
  trick_description: { type: String, required: false },
  tricks: { type: Array, required: true }
});
module.exports = SkateTrick = mongoose.model(
  "skateboard-tricks",
  SkateTrickSchema
);

您可以尝试的一件事是将.lean() 添加到您的查询中

router.get("/", async (req, res) => {
  try {
    let result = await skate_tricks.find({}).lean();
    res.send(result);
  } catch (err) {
    res.send(err);
  }
});

【讨论】:

    【解决方案2】:

    你的结果对象是一个对象数组。

    [
        {
            "trick_type": [],
            "_id": "5f34a5782e0d2fe1c2847633",
            "tricks": [
                {
                    "trick_id": 0,
                    "trick_name": "ollie",
                    "trick_type": [
                        "flat"
                    ],
                    "trick_difficulty": "n",
                    "trick_description": "Fundamental trick involving jumping with the skateboard. Performed by popping the tail with the back foot and sliding the front foot up towards the nose of the board. The sliding action levels the board while you are airborn."
                }
            ]
        }
    ]
    

    因此您必须使用 res.send(result[0].tricks); 访问它或更改您的 API 响应以返回它

    【讨论】:

    • 感谢您的回复,但这对我不起作用。当我这样做时,我得到了一个未定义的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 2014-08-26
    • 2018-01-06
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多