【问题标题】:Argument of type 'DocumentData' is not assignable to parameter of type 'never'“DocumentData”类型的参数不可分配给“never”类型的参数
【发布时间】:2021-10-26 13:26:27
【问题描述】:

我没有找到解决问题的方法: Ts给我一个:

 Argument of type 'DocumentData' is not assignable to parameter of type 'never'

我尝试了在这里找到的解决方案:Argument of type 'DocumentData | undefined' is not assignable to parameter of type 'DocumentData'

但它不起作用。

这是我的功能:

async viewPost (post: { id?: unknown }) {
 const allCommentaires = await commentsCollection.where('postId', '==', post.id).get()
 if (allCommentaires) {
  allCommentaires.forEach(data => {
    const commentaires = data.data()
    commentaires.id = data.id
    if (commentaires !== undefined) {
      this.postComments.push(commentaires)
    }
  })
}
this.fullPost = post
this.showPostModal = true}

我在这一行得到错误:

this.postComments.push(commentaires)

评论者在我上面写的错误下划线

你能帮帮我吗?

【问题讨论】:

    标签: typescript firebase vue.js


    【解决方案1】:

    很奇怪,您链接的解决方案不适合您,但您可以尝试使用 as 关键字声明类型。

    此外,您应该将数据 id 分配移动到 if 语句中,否则您可能会在运行时收到错误 Cannot set property 'id' of undefined

    if (commentaires) {
      commentaires.id = data.id;
      this.postComments.push(commentaires as DocumentData);
    }
    

    【讨论】:

      【解决方案2】:

      我找到了解决方案: 这里的变化:

       async viewPost ({ state, commit }, post: { id?: any }): Promise<void> {
        const postComments: firebase.firestore.DocumentData[] = []
        const allCommentaires = await commentsCollection.where('postId', '==', post.id).get()
        if (allCommentaires) {
          allCommentaires.forEach(data => {
            const commentaires = data.data()
            commentaires.id = data.id
            if (commentaires !== undefined) {
              postComments.push(commentaires)
            }
          })
        }
      },
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-19
        • 2021-03-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-09
        • 2018-11-03
        • 2021-06-09
        相关资源
        最近更新 更多