Answer the question
In order to leave comments, you need to log in
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"
-LpttBMInZYQafSG2pyp
adId:
"-LptqAdOrKUfLJ6adW_B"
done:
false
name:
"name"
phone:
"547478"
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 questionAsk a Question
731 491 924 answers to any question