R
R
Radiss2019-09-29 01:47:05
firebase
Radiss, 2019-09-29 01:47:05

How to solve - Cannot convert undefined or null to object?

"firebase": "^6.6.1",
    "vue": "^2.5.2",
    "vue-router": "^3.0.1",
    "vuetify": "^1.0.0",
    "vuex": "^3.1.1"

Ads (ads) are added to the firebase database, but orders are not displayed in the application.
Error 'Cannot convert undefined or null to object'
in db -
-LpttBMInZYQafSG2pyp
adId: 
"-LptqAdOrKUfLJ6adW_B"
done: 
false
name: 
"name"
phone: 
"547478"

Or, if you log in under a different user, then another user's order is displayed.
store/orders.js
import firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/firestore'
import 'firebase/database'

class Order {
  constructor (name, phone, adId, done = false, id = null) {
    this.name = name
    this.phone = phone
    this.adId = adId
    this.done = done
    this.id = id
  }
}

export default {
  state: {
    orders: []
  },
  mutations: {
    loadOrders (state, payload) {
      state.orders = payload
    }
  },
  actions: {
    async createOrder ({commit}, {name, phone, adId, ownerId}) {
      const order = new Order(name, phone, adId)
      commit('clearError')

      try {
        await firebase.database().ref(`/users/${ownerId}/orders`).push(order)
      } catch (error) {
        commit('setError', error.message)
        throw error
      }
    },
    async fetchOrders ({commit, getters}) {
      commit('setLoading', true)
      commit('clearError')

      const resultOrders = []

      try {
        const fbVal = await firebase.database().ref(`/users/${getters.user.id}/orders`).once('value')
        const orders = fbVal.val()

        Object.keys(orders).forEach(key => {
          const o = orders[key]
          resultOrders.push(
            new Order(
              o.name,
              o.phone,
              o.adId,
              o.done,
              key
            )
          )
        })

        commit('loadOrders', resultOrders)
        commit('setLoading', false)
      } catch (error) {
        commit('setLoading', false)
        commit('setError', error.message)
      }
    },
    async markOrderDone ({commit, getters}, payload) {
      commit('clearError')
      try {
        await firebase.database().ref(`/users/${getters.user.id}/orders`).child(payload).update({
          done: true
        })
      } catch (error) {
        commit('setError', error.message)
        throw error
      }
    }
  },
  getters: {
    doneOrders (state) {
      return state.orders.filter(o => o.done)
    },
    undoneOrders (state) {
      return state.orders.filter(o => !o.done)
    },
    orders (state, getters) {
      return getters.undoneOrders.concat(getters.doneOrders)
    }
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question