使用setDoc()编辑并保存数据到firebase

ddhy6vgd  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(122)

因此,我想将已编辑的项目从数组保存到firebase,但当我尝试使用setDoc()时,我得到以下错误

Uncaught FirebaseError: Expected type 'Ta2', but it was: a custom Aa2 object

这是我的代码:

<div v-for="(post, id) in posts" :key="id">
  <h3>By: {{ post.name }}</h3>
  <p>{{ post.post }}</p>
  <p>{{ post.date }}</p>
    
  <button @click="deletePost(id)">delete</button>
      
  <div v-if="post === postItemToEdit">
    <input type="text" v-model="post.post" >
    <button @click="savePost">Save</button>
    <button @click="cancleEditMode">Cancle</button>
  </div>
      
  <button v-else @click="editPost(post)">Edit</button> 
</div>

<script setup>
  const blogCollectionRef = collection(db, "blogs")

  const name = ref("")
  const post = ref("")
    
  const addPost = () => {
    addDoc(blogCollectionRef, {
      name: name.value,
      post: post.value,
      date: Date.now(),
    });

    name.value = ""
    post.value = ""
  }

  const postItemToEdit = ref()

  const editPost = (post) => {
    postItemToEdit.value = post
  }
  
  const savePost = () => {
    postItemToEdit.value = (false)

    setDoc(blogCollectionRef, {
      post: "Los Angeles"
    })
  }
</script>

我认为这与我使用collection而不是doc这个事实有关,但是我获取的数组存储在我命名为“blogs”的集合中。

8yoxcaq7

8yoxcaq71#

你应该使用updateDoc()来更新一个现有的文档,而不是setDoc()。你需要文档ID,这样你就可以把它传递给savePost()函数:
第一个

相关问题