【问题标题】:I want to know how to propstype to get database like this?我想知道如何通过 propstype 来获取这样的数据库?
【发布时间】:2016-07-06 00:12:52
【问题描述】:
{
    "_id" : ObjectId("577c4b18b1216fd9cb1a2ffc"),
    "username" : "John",
    "todolist" : [ 
        {
            "_id" : ObjectId("577c4b36b1216fd9cb1a2ffd"),
            "newtodo" : {
                "tittle" : "Playing football"
            }
        }
    ],
    "dateAdded" : ISODate("2016-07-06T00:04:40.914Z"),
    "__v" : 0
}

我这样写道具数据库,但标题出错

<p className={styles['author-name']}><FormattedMessage id="by" /> {props.user.username}</p>
      <p className={styles['author-name']}>{props.user.todolist['newtodo.tittle']}</p>

而且我需要知道如何在 Proptypes.shape 中喜欢

user: PropTypes.shape({
 username: PropTypes.string.isRequired,
    todolist:PropTypes.arrayOf(PropTypes.shape({
        newtodo: PropTypes.objectOf(PropTypes.string)({
            tittle:PropTypes.string.isRequired,
        })
})).isRequired,

请帮帮我 T-T 我是 React 的新手

【问题讨论】:

  • 你能更好地描述你收到什么样的错误吗?
  • 实际上我无法从我的数据库(mongodb)中获取(获取)标题值。标题未定义总是有问题。

标签: javascript reactjs arrayofstring


【解决方案1】:

很可能,应该是:

user: PropTypes.shape({
  username: PropTypes.string.isRequired,
  todolist: PropTypes.arrayOf(PropTypes.shape({
    newtodo: PropTypes.shape({
      tittle: PropTypes.string.isRequired
    })
  })).isRequired
})

newtodo 对象应该用PropTypes.shape 描述,而不是Proptypes.objectOf

【讨论】:

  • 谢谢,但是当我在 localhost 上进行测试时,出现这样的错误 Uncaught (in promise) TypeError: Cannot read property 'newtodo.tittle' of undefined
猜你喜欢
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 2023-02-02
  • 2020-01-04
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 2018-10-07
相关资源
最近更新 更多