Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法使用点表示法以JSON中的嵌套对象为目标_Javascript_Node.js_Json_Mongodb_Express - Fatal编程技术网

Javascript 无法使用点表示法以JSON中的嵌套对象为目标

Javascript 无法使用点表示法以JSON中的嵌套对象为目标,javascript,node.js,json,mongodb,express,Javascript,Node.js,Json,Mongodb,Express,我使用mongoose和express来访问MongoDB中的数据,当我引用数据库时可以看到JSON,但是当我尝试将嵌套在其中的对象作为目标时,会收到一条未定义的消息 JS文件以访问数据库。 const express = require('express'); const router = express.Router(); const skate_tricks = require('../models/SkateTrick'); router.get('/', (req, res) =&g

我使用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;
使用邮递员的结果片段

[
    {
        "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.tricks);

结果对象是对象的数组

[
    {
        "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(结果[0].tricks)访问它或更改API响应以返回它,就像尝试向mongoose模型添加
技巧一样

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);
  }
});

似乎
result
是一个数组,您需要使用
result[0]。tricks
来获取值。您的result变量是一个数组,因此您需要访问它
result[0]。tricks
类似this@HaoWu当我尝试用你的方法控制台注销它时,我仍然没有定义。我也注意到了这一点,但不确定它为什么不起作用。也许您可以尝试
console.log(JSON.parse(JSON.stringify(result))
来查看它的实际功能it@HaoWu我在控制台上得到了相同的结果。它显示数据被包装在带有方括号的数组中。我以前使用过JSON,但从来没有遇到过这个问题。谢谢你的回复,但这对我来说不起作用。当我这样做的时候,我得到了一个未定义的值。