You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Nuxt.js结合NuxtFirebase获取Firebase Firestore文档ID

解决Firestore获取数据时缺失文档ID的问题

你只需要在遍历文档列表时,把文档ID合并到返回的数据对象中即可,修改你的postList action代码如下:

const actions = {
  // 获取Post列表
  async postList({ commit }) {
    try {
      const req = await this.$fire.firestore.collection('Post').get()
      // 合并文档ID和数据内容
      const posts = req.docs.map((doc) => ({
        id: doc.id,
        ...doc.data()
      }));

      console.log(posts)
      commit("SET_POST", posts)
    } catch (e) {
      console.log(e)
    }
  }
};

export default {
  state,
  getters,
  mutations,
  actions,
}

修改后,每个返回的post对象都会包含id字段(对应Firestore中的文档ID),同时保留原来的所有内容字段。

内容的提问来源于stack exchange,提问作者Udemezue John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 12:45:24