首页 > 解决方案 > 无法将新文档设置为 Firestore 中的集合

问题描述

我已经尝试了将新文档设置到 Firestore 的各种不同方式,但无法实现。我没有收到任何错误,但数据永远不会发布。我确定我错过了一些简单的东西,但我就是不知道它是什么

import styles from '../../styles/createPost.module.css'
import firebase from '../../firebase/initFirebase'
import { useSession } from 'next-auth/client'

export default function CreatePostButton(props) {
  const [session] = useSession()
 
  const time = Date.now()
  const today = new Date(time)

  const sendData = async() => {
    if (props.postContent) {  
        await firebase.firestore().collection("postData").doc()
        .set({
          postAuthor: session.user.name,
          path: "/cidProfile",
          profileImg: session.user.image,
          postText: props.postText,
          postContent: props.postContent.name,
          postTime: today.toUTCString(),
          reactionsTotal: 1,
          isliked: false,
          commentCount: 0,
          shareCount: 0,
          sharable: props.shareable,
        }).then(alert('Your Post was Sent!'))
    } else {
        await firebase.firestore().collection("postData").doc()
          .set({
            postAuthor: session.user.name,
            path: "/meow",
            profileImg: session.user.image,
            postText: props.postText,
            postTime: today.toUTCString(),
            reactionsTotal: 1,
            isliked: false,
            commentCount: 0,
            shareCount: 0,
            sharable: props.shareable,
          }).then(alert(props.postText))
      } 
  }

  return (
    <div className={styles.postButtonDiv}>
      <button className={styles.postButton} onClick={sendData} disabled={!props.disableButton}>Post</button>
    </div>
  )
}

标签: javascriptfirebasegoogle-cloud-firestore

解决方案


语法似乎很好,我怀疑这要么是安全规则问题,要么是 Firebase 客户端未启动的问题。这些错误自然会被记录或在您的环境控制台中抛出错误。


推荐阅读