我試圖將帖子添加到我的 firebase 資料庫,但我在標題中遇到錯誤(未捕獲(承諾中)FirebaseError:使用無效資料呼叫的函式 addDoc()。不支持的欄位值:未定義(在檔案帖子/5TT9b7F0lkqxoivmsIiH 的欄位標題中找到) )。這是我的代碼:
import React, { Component } from 'react';
import { addDoc, collection } from 'firebase/firestore';
import { db } from '../firebaseconf';
class CreatePost extends Component {
constructor(props) {
super(props);
this.state = {
setTitle: "",
setPostText: ""
};
}
sTitle = (event) => {
this.setState({ setTitle: (event.target.value) });
}
sPostText = (event) => {
this.setState({ setPostText: (event.target.value) });
}
collectionRef = collection(db, "posts");
createPost = async () => {
await addDoc(this.collectionRef, { title: this.setTitle, postText: this.setPostText });
}
render() {
return (
<div className="cpPage">
<div className="cpContainer">
<h1>Create a Post</h1>
<div className="inputGp">
<label>Title:</label>
<input
placeholder="Title..."
onChange={this.sTitle}
/>
</div>
<div className="inputGp">
<label>Post:</label>
<textarea
placeholder="Write your post..."
onChange={this.sPostText}
/>
</div>
<button onClick={this.createPost}>Add your post</button>
</div>
</div>
);
}
}
export default CreatePost;
uj5u.com熱心網友回復:
Firebase Firestore 不接受undefined作為檔案欄位之一的值(title在您的情況下)。相反,您可以使用null.
(未經測驗)示例:
await addDoc(this.collectionRef, {
title: this.setTitle || null,
postText: this.setPostText || null,
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/421176.html
標籤:
