A
A
Alexander Belkevich2018-05-09 14:17:15
JavaScript
Alexander Belkevich, 2018-05-09 14:17:15

How to get object id from firebase?

Hello. Question from a newbie to Vue. I decided to attach it to my firebase test project here. I want to make an impromptu news feed.
We click on the news - we open the news component with the required ID (using route).
And here is the question of how to correctly generate this news ID.
Now everything works for me like this (I use vuex)

let db = fb.database()
let refBlogs = db.ref('blogs')

class BlogElement {
  constructor (name, description, id = null) {
    this.name = name
    this.description = description
    this.id = id
  }
}

mutations: {
    createBlogElement (state, payload) {
      state.blog.push(payload)
      const blogElement = new BlogElement(payload.name, payload.description)
      refBlogs.push(blogElement)
    }
  }

In the constructor I have an ID. What is the best way to fill it? Somewhere I saw a feint with setting the key from firebase to this id, but I can’t find this moment and is it generally correct ...
PS As far as I understand, it is better to do access to obtaining and creating data through actions? Through async/away?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hopeful_romantic, 2018-05-09
@AlexBelkevich

refBlogs.push(blogElement)
  .then((data) => {
     let key = data.key
     refBlogs(blogElement).update({
       'id': key
     })
  })

M
Moe Green, 2018-05-10
@mQm

axios.get(awesome_url)
    .then(res => {
        const news = Object.keys(res.data).map(el => {
            return {
                ...res.data[el],
                id: el
            }
        })
        this.news = news
    })
    .catch(e => console.log(e))

Something like this?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question