简体   繁体   English

Javascript/Typescript - 不能 map 把所有东西都放到一张桌子上

[英]Javascript/Typescript - Can't map everything to a table

What I find most interesting is that {users.username} works, same with {users.profile.avatar} and even {users.profile.bio} but not what I need most of all: {users.emails.verified} and {users.emails.address}我发现最有趣的是{users.username}{users.profile.avatar} users.profile.avatar} 甚至{users.profile.bio} users.profile.bio} 一样有效,但不是我最需要的: {users.emails.verified}{users.emails.address}

I'm guessing that it has to do with the mapping of the data?我猜它与数据的映射有关? Perhaps it's how I'm trying to call it?也许这就是我试图称呼它的方式? I tried {users['emails']['address']} as well.. but it doesn't work either.我也试过{users['emails']['address']} .. 但它也不起作用。 yet it works for {users['profile']['bio']} which makes it a bit bigger of a headache for me.但它适用于{users['profile']['bio']}这让我有点头疼。 Any help would be awesome, I'm all ears as to learning how to go about this!任何帮助都会很棒,我全神贯注于学习如何 go !

在此处输入图像描述

import React from 'react';
import { Redirect } from 'react-router-dom';

import Avatar from '@atlaskit/avatar';
import DropdownMenu, {
  DropdownItemGroup,
  DropdownItem,
} from '@atlaskit/dropdown-menu';
import Tag, { TagColor } from '@atlaskit/tag';
import DynamicTable from '@atlaskit/dynamic-table'
import PageHeader from '@atlaskit/page-header';

import gql from 'graphql-tag';
import { useQuery } from '@apollo/react-hooks';

import Button, { ButtonGroup } from '@atlaskit/button';

const GET_USERS = gql`
  query getUsers {
      getUsers {
        id
        username
        isAdmin
        emails {
          address
          verified
        }
        profile {
          bio
          avatar
        }
      }
      getUser {
        id
      }
    }
`;

const Users = () => {

  const actionsContent = (
    <ButtonGroup>
      <Button appearance="primary">Search Box Here Here</Button>
    </ButtonGroup>
  );

  const { loading, error, data } = useQuery(GET_USERS);

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error: {error.message}</p>;

  if (!data.getUser) {
    return <Redirect to="/login" />;
  }

  const createHead = (withWidth: boolean) => {
    return {
      cells: [
        {
          key: 'avatar',
          content: 'Avatar',
          isSortable: false,
          width: withWidth ? 2 : undefined,
        },
        {
          key: 'username',
          content: 'Username',
          isSortable: true,
          width: withWidth ? 18 : undefined,
        },
        {
          key: 'email',
          content: 'Email',
          shouldTruncate: true,
          isSortable: true,
        },
        {
          key: 'tags',
          content: 'Tags',
        },
        {
          key: 'action',
          content: 'Action',
          shouldTruncate: true,
          width: withWidth ? 2 : undefined,
        },
      ],
    };
  };
  
  const head = createHead(true);

  var users = data.getUsers;
  
  console.log(users)

  for(let i = 0, l = users.length; i < l; i++) {

    var rows = users.map((user: any) => ({
      cells: [
        {
          key: 'avatar',
          content: (
            <span style={{ display: 'flex', alignItems: 'center' }}>
              <div style={{ marginRight: 8 }}>
                <Avatar
                  name={user.username}
                  size="small"
                  src={user.profile.avatar}
                />
              </div>
            </span>
          ),
        },
        {
          key: 'user',
          content: (
            <span style={{ display: 'flex', alignItems: 'center' }}>
              {user.emails.address}
              {user.emails.verified}
            </span>
          ),
        },
        {
          key: 'email',
          content: (
            <span style={{ display: 'flex', alignItems: 'center' }}>
              {user.isAdmin}
              <Tag text="Verified" color="greyLight" />      
            </span>
          ),
        },
        {
          key: 'tags',
          content: (
            <span style={{ display: 'flex', alignItems: 'center' }}>
              {user.isAdmin}
              <Tag text="Admin" color="grey" />      
            </span>
          ),
        },
        { 
          key: 'lols',
          content: (
            <Button>More</Button>
          ), 
        },
      ],
    }))

  }
  return (
    <div>
      <PageHeader 
        actions={actionsContent}
      >        
        Users
      </PageHeader> 

      <DynamicTable
        head={head}
        rows={rows}
        isLoading={false}
        defaultSortOrder="ASC"
        loadingSpinnerSize="large"
      />  

    </div>
  );
}

export default Users;

Well, I guess I could mark this question answered!好吧,我想我可以将这个问题标记为已回答!

Another reason to define my types properly:正确定义我的类型的另一个原因:

Ah.啊。 Just noticed the console log in the image, emails seems to be an array of email objects and not an object.刚刚注意到图像中的控制台日志,电子邮件似乎是 email 对象的数组,而不是 object。 so instead of {user.emails.verified} you should have {user.emails[0],verified}.所以你应该有 {user.emails[0],verified} 而不是 {user.emails.verified}。 same goes for address, Also, you may want to check if emails actually contains something and/or if it contains more than one email objectibrahim mahrir地址也是如此,此外,您可能需要检查电子邮件是否实际包含某些内容和/或是否包含多个 email objectibrahim mahrir

So for my specific use case: {user.emails[0].address} was what I was looking for as well as {String(user.emails[0].verified)} for returning boolean values!因此,对于我的特定用例: {user.emails[0].address}是我正在寻找的以及{String(user.emails[0].verified)}用于返回 boolean 值!

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

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