[英]Error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead
I'm having this error message, which I am not really sure how to handle.我收到此错误消息,我不确定如何处理。 Technically, what I want to do is to upload an image into the database and then display it.
从技术上讲,我想做的是将图像上传到数据库中,然后显示。
Each time I try to upload an image, this appears.每次我尝试上传图片时,都会出现这种情况。 The image has successfully been uploaded to the database, but it is giving me this error instead of rendering the image on the screen.
图像已成功上传到数据库,但它给了我这个错误,而不是在屏幕上呈现图像。
I have defined setPosts like this:我已经定义了这样的 setPosts:
const [posts, setPosts] = useState([]);
I'm guessing the problem is related to the database since on Firebase it looks like this, which I'm certain it's not supposed to.我猜这个问题与数据库有关,因为在 Firebase 上它看起来像这样,我确定它不应该这样。
Here is the code for UploadImage.js:这是 UploadImage.js 的代码:
const handleUpload = () => {
const uploadTask = storage.ref(`images/${image.name}`).put(image);
uploadTask.on(
() => {
storage
.ref("images")
.child(image.name)
.getDownloadURL()
.then((url) => {
db.collection("posts").add({
timestamp:firebase.firestore.FieldValue.serverTimestamp(),
caption: caption,
imageUrl: url,
username: username
});
setCaption("");
setImage(null);
});
}
);
};
I'm kinda new to ReactJS and Firebase.我对 ReactJS 和 Firebase 有点陌生。 Any help would be appreciated!
任何帮助,将不胜感激!
UPDATE : this is how i render "Post" in App.js更新:这就是我在 App.js 中呈现“发布”的方式
{
posts.map(({id, post}) => (
<Post key={id} postId={id} user={user} username={post.username} caption={post.caption} imageUrl={post.imageUrl}/>
))
}
I had this same issue with this same code-along.我在使用相同的代码时遇到了同样的问题。 Your question helped me figure out the answer.
你的问题帮助我找到了答案。
Go to your firebase console and check each document (the "posts"). Go 到您的 firebase 控制台并检查每个文档(“帖子”)。 You probably have some from earlier in the code-along.
你可能在代码的前面有一些。 Check the 'username' for each document.
检查每个文档的“用户名”。 If any of them are an object (they have a down arrow that expands to show another 'username' that is type (string)), delete those.
如果其中任何一个是 object(它们有一个向下箭头,可以展开以显示另一个类型为(字符串)的“用户名”),请删除它们。 After doing that, it left me with my most recent upload attempt (you might have to drop all of your earlier 'posts') - but then it worked.
这样做之后,它给我留下了我最近的上传尝试(你可能不得不放弃你之前的所有“帖子”)——但后来它奏效了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.