首页 > 解决方案 > React Native - JSON对象的数组属性返回未定义,其他属性都返回一个值

问题描述

我对 React Native 很陌生,我正在努力弄清楚为什么我不能访问 JSON 对象的数组属性。

我的 JSON 对象如下所示:

{
    "id": 1,
    "items:": [
        {
            "description": "What is the current vehicle mileage?",
            "id": 1,
            "required": true,
            "title": "Mileage",
            "type": "Number"
        },
        {
            "description": "First aid kid present?",
            "id": 2,
            "required": true,
            "title": "Safety",
            "type": "Boolean"
        }
    ],
    "title": "Default Inspection Report"
}

我正在使用的代码是:

import React from "react";
import { View, Text } from "react-native";

export default function VehicleInspection({ navigation, route }) {
  console.log(route.params.template.items);
  console.log(route.params.template);
  return (
    <View><Text>Title: {route.params.template.title}</Text></View>
  );
}

如果我登录route.params.template到控制台,它会返回包含“items”数组内容的整个对象。如果我登录route.params.template.items到控制台,它会返回未定义。

在视图组件中返回route.params.template.title按预期返回“默认检查报告”。

我在这里错过了一些非常愚蠢的东西吗?

这是上面代码的控制台输出:

undefined
Object {
  "id": 1,
  "items:": Array [
    Object {
      "description": "What is the current vehicle mileage?",
      "id": 1,
      "required": true,
      "title": "Mileage",
      "type": "Number",
    },
    Object {
      "description": "First aid kid present?",
      "id": 2,
      "required": true,
      "title": "Safety",
      "type": "Boolean",
    }
  ],
  "title": "Default Inspection Report",
}

标签: javascriptjsonreact-native

解决方案


推荐阅读