R
R
Radiss2019-09-19 21:23:11
firebase
Radiss, 2019-09-19 21:23:11

How to solve the problem with creating ads in Firebase?

I'm trying to create a SPA application in firebase. But there may be an error in working with versions, but I won’t figure out what’s wrong and how to connect to the new one.
"firebase": "^6.6.1",
"vue": "^2.5.2",
"vue-router": "^3.0.1",
"vuetify": "^1.0.0",
"vuex": "^3.1.1"
When trying to create an ad, it gives an error

Reference.push failed: first argument contains undefined in property 'ads.ownerId'

ads.js
spoiler

import * as fb from 'firebase'

class Ad {
  constructor (title, description, ownerId, imageSrc = '', promo = false, id = null) {
    this.title = title
    this.description = description
    this.ownerId = ownerId
    this.imageSrc = imageSrc
    this.promo = promo
    this.id = id
  }
}

export default {
  state: {
    ads: [
      {  },
   ]
  },
  mutations: {
    createAd (state, payload) {
      state.ads.push(payload)
    }
  },
  actions: {
    async createAd ({commit, getters}, payload) {
      commit('clearError')
      commit('setLoading', true)

      try {
        const newAd = new Ad(
          payload.title,
          payload.description,
          getters.user.id,
          payload.imageSrc,
          payload.promo
        )

        const ad = await fb.database().ref('ads').push(newAd)

        commit('setLoading', false)
        commit('createAd', {
          ...newAd,
          id: ad.key
        })
      } catch (error) {
        commit('setError', error.message)
        commit('setLoading', false)
        throw error
      }
    }
  },
  getters: {
    ads (state) {
      return state.ads
    },
    promoAds (state) {
      return state.ads.filter(ad => {
        return ad.promo
      })
    },
    myAds (state) {
      return state.ads
    },
    adById (state) {
      return adId => {
        return state.ads.find(ad => ad.id === adId)
      }
    }
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Aleks_Ko, 2020-08-25
@radiss

Yes, the lesson is the same - as for me, not bad, but outdated ....
Just like you, I had an error with ownerId, for some reason getters.user.id added the user id to the constructor for the first time, and then he did not want to do it and gave out undefined.
I decided this way:
instead of getters.user.id in the ads.js ( createAd ) file, I inserted fb.auth().currentUser.uid, the user id is now added to me normally ....

V
Valto, 2020-10-23
@Valto

Here is another example of how you can solve this moment.
But the course is really already outdated in some things.
Moreover, version 3 of Vue has already been released.
Forum Link

A
Alex, 2020-02-07
@orlovec

Looks like we have the same lessons.
I just got to that too. And the Internet is full of questions with this error, only there is no answer. On the official website, the author did not answer anything sensible.
How did you solve the problem? There is something with the routes, as I understand it ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question