简体   繁体   English

无法使用点表示法定位 JSON 中的嵌套对象

[英]Unable to target a nested object in JSON by using dot notation

I am using mongoose and express to access data within MongoDB and I can see the JSON when I make a reference to the database, but when I try to target an object nested inside, I get an undefined message.我正在使用 mongoose 和 express 访问 MongoDB 中的数据,当我引用数据库时,我可以看到 JSON,但是当我尝试定位嵌套在其中的对象时,我收到一条未定义的消息。

JS file to access DB. 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;

Results snippet using Postman使用 Postman 的结果片段

[
    {
        "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."
            }
        ]
    }
]

I want to access the "tricks" array of objects directly, but it's not working.我想直接访问对象的“技巧”数组,但它不起作用。

res.send(result.tricks);

You result object is an array of object.你的结果对象是一个对象数组。

[
    {
        "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."
            }
        ]
    }
]

So you have to access it by using res.send(result[0].tricks);所以你必须使用res.send(result[0].tricks);来访问它res.send(result[0].tricks); or change your API response to return it like或更改您的 API 响应以返回它

Try to add tricks to your mongoose model尝试为您的猫鼬模型添加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
);

One thing what you can try is to add .lean() to your query您可以尝试的一件事是将.lean()添加到您的查询中

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

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

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