首页 > 解决方案 > TypeError:绑定标签可能为空的json数据时无法读取null的属性'map'

问题描述

我的数据库中有一些记录,但并非所有记录都有标签,有些可能是空的,它们存储在 CosmosDb 上并作为 Json 数组返回。

我正在使用 antd 表和标签: https ://ant.design/components/table/

示例:标签

我有以下代码:

import React, { Component } from 'react';
import {  Table, Tag} from 'antd';
import { adalApiFetch } from '../../adalConfig';
import Notification from '../../components/notification';

class ListPageTemplatesWithSelection extends Component {

    constructor(props) {
        super(props);
        this.state = {
            data: []
        };
    }

    fetchData = () => {
        adalApiFetch(fetch, "/PageTemplates", {})
          .then(response => response.json())
          .then(responseJson => {
            if (!this.isCancelled) {
                const results= responseJson.map(row => ({
                    key: row.Id,
                    Name: row.Name,
                    SiteType: row.SiteType,
                    Tags: row.Tags
                  }))
              this.setState({ data: results });
            }
          })
          .catch(error => {
            console.error(error);
          });
      };


    componentDidMount(){
        this.fetchData();
    }

    render(){
        const columns = [
                {
                    title: 'Id',
                    dataIndex: 'key',
                    key: 'key',
                }, 
                {
                    title: 'Name',
                    dataIndex: 'Name',
                    key: 'Name',
                }, 
                {
                    title: 'Site Type',
                    dataIndex: 'SiteType',
                    key: 'SiteTy[e',
                },{
                  title: 'Tags',
                  key: 'Tags',
                  dataIndex: 'Tags',
                  render: Tags => (
                    <span>
                      {Tags.map(tag => {
                        let color = tag.length > 5 ? 'geekblue' : 'green';
                        if (tag === 'loser') {
                          color = 'volcano';
                        }
                        return <Tag color={color} key={tag}>{tag.toUpperCase()}</Tag>;
                      })}
                    </span>
                  ),
                }
        ];

        const rowSelection = {
            selectedRowKeys: this.props.selectedRows,
            onChange: (selectedRowKeys) => {
              this.props.onRowSelect(selectedRowKeys);
            }
          };


        return (
            <Table rowSelection={rowSelection}  columns={columns} dataSource={this.state.data} />
        );
    }
}

export default ListPageTemplatesWithSelection;

但是我有这个错误:

 58 | dataIndex: 'Tags',
  59 | render: Tags => (
  60 |   <span>
> 61 |     {Tags.map(tag => {
  62 |       let color = tag.length > 5 ? 'geekblue' : 'green';
  63 |       if (tag === 'loser') {
  64 |         color = 'volcano';

我不确定这个错误是否是因为某些行没有标签,或者如果错误是由于不同的原因,无论如何,我不确定我应该怎么做才能避免这种情况。

标签: javascriptreactjsreduxantd

解决方案


如果标签可能为空,那么您必须添加一个检查,以便您不会映射到它以呈现

<span>
  {Tags && Tags.map(tag => {
    let color = tag.length > 5 ? 'geekblue' : 'green';
    if (tag === 'loser') {
      color = 'volcano';
    }
    return <Tag color={color} key={tag}>{tag.toUpperCase()}</Tag>;
  })}
</span>

推荐阅读