Answer the question
In order to leave comments, you need to log in
Outputting data for list from data array with vuex (store.js)?
There is a small vue project codesandbox
A database was created in firebase from where I pull data to display the product
firebase list:
const config = {
...
};
firebase.initializeApp(config);
// firebase utils
const db = firebase.firestore();
// firebase collections
const usersCollection = db.collection("users");
const locationsCollection = db.collection("locations");
export { firebase, db, usersCollection, locationsCollection };
import { firebase, locationsCollection } from "@/firebase";
export default new Vuex.Store({
state: {
appTitle: "Landing Page",
locations: []
},
mutations: {
setLocations(state, payload) {
state.locations = payload;
},
setUserLocations(state, payload) {
state.userLocations = payload;
}
},
actions: {
async getLocations({ state, commit }) {
locationsCollection
.get()
.then(querySnapshot => {
let locationsArray = [];
querySnapshot.forEach(doc => {
let location = doc.data();
location.id = doc.id;
locationsArray.push(location);
});
commit("setLocations", locationsArray);
})
.catch(() => {
commit("setLocations", []);
});
},
addLocation({ state }, payload) {
firebase
.database()
.ref()
.child("/locations")
.push(payload);
//console.log(payload);
},
getUserLocations({ state, commit }) {
return firebase
.database()
.ref("/locations")
.once("value", snapshot => {
commit("setUserLocations", snapshot.val());
});
}
},
getters: {}
});
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