gpt4 book ai didi

javascript - 在 React Native 中使用子字符串时出现错误 'undefined is not an object'

转载 作者:行者123 更新时间:2023-11-28 04:39:32 24 4
gpt4 key购买 nike

我是 React Native 新手。

作为实践,我正在构建一个简单的 React Native 应用程序,在其中连接到 HubSpot 的 API 来检索一些博客文章、显示它们并与它们交互。目前一切正常,但当我解析 JSON 时,我想出了一个包含时间戳的字段“publish_date”。由于某种原因,这个时间戳包含 3 个额外的数字,我认为这是针对时区的。无论如何,当我使用“react-timestamp”输出时间戳时,我使用这个值并且得到的日期太远了。所以我想到使用子字符串删除最后3位数字并输出正确的日期。

这是失败的代码的一部分:

renderRow(post) {
return(
<View style={styles.row}>
<Image style={styles.image} source={{uri: post.featured_image}} />
<View style={styles.info}>
<Text style={styles.title}>{post.title}</Text>
<Text style={styles.author}>Por {post.author_name}</Text>
<Timestamp time={post.publish_date.substring(10)} component={Text} format='date' />
</View>
</View>
);
}

render() {
return(
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
style={styles.page}
/>
);
}

当我运行它时,我收到下一个错误:

Error

我不知道我做错了什么,这对我来说是全新的。昨天我尝试使用 .replace('') 时遇到了同样的错误,所以我放弃并寻求另一个解决方案。在这种情况下,我想我不能再做任何事情了。

最佳答案

尝试记录您的帖子对象并查看是否定义了“publish_date”

根据错误信息,传递给函数renderRow的对象post上没有“publish_date”属性,因此,对substring的调用失败。

关于javascript - 在 React Native 中使用子字符串时出现错误 'undefined is not an object',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43899006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com