简体   繁体   中英

How to access data ID in fIrebase

I am working with Firebase Firestore using Nuxt.js and I want to retrieve data from my database. I am using firebase with NuxtFirebase, the official plugin for firebase.

I need to access the ID property from my firebase database

Here is my store

const actions = {

  // The Post list API 
  async postList({ commit }) {
    try {
      const req = await this.$fire.firestore.collection('Post').get()
      const posts = req.docs.map((doc) => doc.data());

      console.log(posts)
      commit("SET_POST", posts)

    } catch (e) {
      console.log(e)
    }
  }
};


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

Looking at my console I get the following data

[
    {
        "title": "Bitcoin is the Detector of Imbeciles",
        "created_at": {
            "seconds": 1672921367,
            "nanoseconds": 80000000
        },
        "featured_image": "https://firebasestorage.googleapis.com/v0/b/newsly-df76d.appspot.com/o/photo-1565402170291-8491f14678db.jpg?alt=media&token=535d7444-15ad-4f28-af8b-4e3918794c86",
        "content": "Last year, 2022 was not of much respite for cryptocurrencies. While bitcoin has lost more than 60% of its value, the entire sector is in crisis, punctuated by various bankruptcies such as those of Terra and FTX.",
        "tag": "Business"
    },
    {
        "featured_image": "https://firebasestorage.googleapis.com/v0/b/newsly-df76d.appspot.com/o/photo-1519311965067-36d3e5f33d39.jpg?alt=media&token=4d49b9db-ec6b-4443-81a1-c52bc71bbd06",
        "title": "How senior product managers think differently",
        "created_at": {
            "seconds": 1672921319,
            "nanoseconds": 639000000
        },
        "content": "I often got asked how a product manager could get promoted to a more senior level. The truth is, getting a promotion is often a complicated game. Yes, your skills and achievements play a role, but so do other factors such as how much your manager cares about developing talents, how good and tenured your peers are, how political the company is, etc.",
        "tag": "Business"
    },
    {
        "content": "ChatGPT is blowing up. Twitter is inundated with screenshots of the app, coding sites like Stack Overflow are already banning answers produced with it, and over 1 million people have played with it. It’s a sensation.  As a professional AI researcher, I wouldn’t have called that. ChatGPT is trained specifically to act as a chat bot, but fundamentally it’s using the same GPT-3 technology that’s been available for over two years now.",
        "created_at": {
            "seconds": 1672921101,
            "nanoseconds": 807000000
        },
        "title": "ChatGPT Is Having a Thomas Edison Moment",
        "tag": "Technology",
        "featured_image": "https://firebasestorage.googleapis.com/v0/b/newsly-df76d.appspot.com/o/istockphoto-178586669-170667a.jpg?alt=media&token=f8c15944-080b-40f7-a926-589a8ff07bb4"
    }
]

I need to access the ID

The req is a QuerySnapshot and every doc in the map is a QueryDocumentSnapshot that has id property. Try:

const posts = req.docs.map((doc) => ({ id: doc.id, ...doc.data() }));
console.log(posts)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM