I am using firebase and I am trying to get the previous value of the updated data using 'onSnapshot' method but I couldn't find a way. I know that before version 9 of firebase trigger functions like 'onUpdate' were returning a change object and we were able to reach to both old and new data from that change object but I couldnt figure out a way in the SDK 9 of firebase. I would appreciate if anyone could help me out.
Here is my code snippet:
import { getFirestore, collection, doc, onSnapshot, query } from 'firebase/firestore'
import { initializeApp } from 'firebase/app';
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: ""
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
/**
* Trigger function that fires on update of any document on cafe collection
*/
export const onUpdate = async () => {
const cafeCollection = collection(db, 'cafes');
const q = query(cafeCollection);
onSnapshot(q, (snapshot) => {
let changesArr = snapshot.docChanges();
if(changesArr.length == 1){
let changes = changesArr[0];
console.log(changes.type); // added-modified-removed
console.log(changes.doc.id); // id of the updated doc
console.log(changes.doc.data()); // newly updated data
}
})
}
I know that before version 9 of firebase trigger functions like 'onUpdate' were returning a change object
A change object is only available in Cloud Functions, as shown in the documentation on Writing Cloud Firestore-triggered functions. It is not (nor ever was) available in the client-side SDKs.
If you want to compare the current and previous snapshot, you will have to introduce a variable where you keep the previous snapshot yourself.
Related
I made a Firebase app with database, connected it with Vue app, which is like todo-list. I tried to get a data from database from firebase, which is used to make components for app, but something got wrong. Vue app doesn't get the data from firebase, so items aren't created
It's th error:
#firebase/firestore: Firestore (9.10.0): Uncaught Error in snapshot listener: {"code":"permission-denied","name":"FirebaseError"
Initializing firebase:
import { initializeApp } from "firebase/app";
import { getDatabase } from "firebase/database";
import { getFirestore } from "#firebase/firestore";
const firebaseConfig = {
apiKey: "AIzaSyBZlP_E74i40ZLopttFlUbg3j36wirKf9A",
authDomain: "quotes-app-8788c.firebaseapp.com",
projectId: "quotes-app-8788c",
storageBucket: "quotes-app-8788c.appspot.com",
messagingSenderId: "934346412075",
appId: "1:934346412075:web:5da2928fecb05c834f5a6e"
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
export {
db
}
Here is my code:
onSnapshot(collection(db, 'quotes'), (QuerySnapshot) => {
const fbquotes = [];
QuerySnapshot.forEach((doc) => {
const quote = {
id: doc.data().id,
quoteText: doc.data().quoteText,
quoteAuthor: doc.data().quoteAuthor,
quoteGenre: doc.data().quoteGenre,
timeCreating: doc.data().timeCreating,
timeEditing: doc.data().timeEditing
}
this.fbquotes.push(quote)
})
this.quotes = fbquotes
console.log(this.quotes);
})
console.log(this.quotes);
},
The error code "permission-denied" tells that your security rules are not allowing the user to query that whole collection. If a user can read all documents from the "quotes" collection then make sure you have the following rules:
match /quotes/{quotesId} {
allow read: if true;
}
You can change the condition for allow read if your requirements are different. Also Security Rules are not filters so if you want to allow a user to read their own documents only, then make sure you change the query in your code as well to match the security rules.
Hello I am trying to configure react app with firebase and use firestore.
"firebase": "^9.1.3"
I followed the instructions given in official docs.
Here is my congig.js file.
import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
const firebaseConfig = {
apiKey: '****',
authDomain: '*****',
projectId: '*****',
storageBucket: '*****',
messagingSenderId: '****',
appId: '*****',
};
const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);
I am sure this gets initialized.
When I export it and use it in other file. collection is greyed out in vs code that means i am not using the import.
databaseservice.js
import { db } from './config';
import { collection, doc } from 'firebase/firestore';
export const getChapters = (scanId) => {
db.collection('somecollection')
.doc(scanId)
.get()
.then((doc) => {
if (doc.exists) {
console.log('Document data:', doc.data());
} else {
// doc.data() will be undefined in this case
console.log('No such document!');
}
})
.catch((error) => {
console.log('Error getting document:', error);
});
};
Error:TypeError: config__WEBPACK_IMPORTED_MODULE_0_.db.collection is not a function
I have tried with compat and lite versions. Getting the same issue.
This is v8/compat syntax:
db.collection('somecollection')
.doc(scanId)
.get()
.then((doc) => {
In v9/modular syntax, the equivalent is:
getDoc(doc(db, 'somecollection', scanId))
.then((doc) => {
For converting this type of thing, I find it easiest to keep the Firebase documentation and upgrade guide handy.
Firebase have changed their API to new modular syntax in version 9. You are using old syntax from version 8. You can read more about this and find instructions on upgrading your code here: https://firebase.google.com/docs/web/modular-upgrade
Also, everywhere in Firebase documentation, they now have 2 separate examples: one for old syntax, one or new modular syntax: https://firebase.google.com/docs/firestore/query-data/get-data
In firestore, a field is like a column in mysql. How can I add a field in every existing document in Firestore using Javascript?
I am interested to add an empty array field in every document.
I thought this is common but somehow I could not find any relevant info.
const firebase = require("firebase");
require("firebase/firestore");
firebase.initializeApp({
apiKey: "XXXX",
authDomain: "xxxx",
projectId: "xxxx"
});
var db = firebase.firestore();
function getMyDocs() {
const snapshot =
firebase.firestore().collection('testusers').get()
return snapshot.docs.map(doc => {
db.collection("testusers").doc(doc.id).update({
title: "Testing"
})
});
};
getMyDocs();
This is a nodejs code but I am getting TypeError: Cannot read property 'map' of undefined
You have to fetch all docs and update each one seperatly.
JS Code:
async getMyDocs() {
const snapshot = await db.collection('myCollection').get()
return snapshot.docs.map(doc => {
db.collection("myCollection").doc(doc.id).update({myNewField:"fieldValue"})
});
}
Firestore is nosql.There are no columns or rows.
Instead, you store data in documents, which are organized into collections.
Each document contains a set of key-value pairs. The structure is dynamic.
You can think it like a big json object.
Good day developers, I'm building this app in Vuejs with Firebase, storing some videos and images in my Storage module of Firebase, but then when I initialize the function of retrieving that images I got this error: "firebase__WEBPACK_IMPORTED_MODULE_2__.storage(...).ref(...).child(...).then is not a function"
On my Vuex method, I set this in order to access the image in my firebase storage
actions: {
getVideoAboutMe({commit}) {
firebase
.storage()
.ref("myVideo")
.child("coverr-a-greek-cemetery-top-view-1578925848736.mp4")
.then(uploadTaskSnapshot => {
commit("setVideoAboutMe", uploadTaskSnapshot);
console.log(uploadTaskSnapshot);
return uploadTaskSnapshot.ref.getDownloadURL();
})
.catch((error) => {
alert("Error", error);
});
},
And my main js file is designed on this way:
import * as firebase from "firebase";
import "firebase/storage";
Vue.config.productionTip = false;
Vue.component("myAboutMe", aboutTag);
new Vue({
router,
store,
vuetify,
render: h => h(App),
created() {
firebase.initializeApp({
apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
authDomain: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
databaseURL: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
projectId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
storageBucket: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
});
this.$store.dispatch("getVideoAboutMe");
}
}).$mount("#app");
then all the logic on components is created, but the error refers exactly to this method in the vuex , like if storage didn't exist(i assume it does cause of importing the firebase).
Any advice about how could I improve this?...thanks!!!
child() doesn't return a promise, so you can't call then() on it. It returns a Reference object. If you want to get the download URL for the object referred to by that reference, just call getDownloadURL() on it directly. It returns a promise.
firebase
.storage()
.ref("myVideo")
.child("coverr-a-greek-cemetery-top-view-1578925848736.mp4")
.getDownloadURL()
.then(...)
.catch(...)
I want to send an Item to my database collection in firestore, for some reason the add() function gets executed twice so the same object is sent two times with different autogenerated Id, which is obviously not the desired behavior, below is the file from which I initialize my firebase app.
import firebase from 'firebase'
// Initialize Firebase
var config = {
apiKey: "**********",
authDomain: "*******",
databaseURL: "https://*****.firebaseio.com",
projectId: "my-****",
storageBucket: "****.appspot.com",
messagingSenderId: "*******"
};
const firebaseApp = firebase.initializeApp(config);
firebaseApp.firestore().settings({ timestampsInSnapshots: true })
export default firebase.firestore()
now this code below is how i trigger the add() function to send the item to the database from my .vue file
import db from '../firebase/init.js';
export default {
...
methods: {
addNewItem(){
let item = {
row1: 'record one,
row2: 'record two,
}
db.collection('myItems').push(item)
.then(doc => console.log(doc))
.catch(err => console.log(err))
}
}
}
please let me know in a comment below if any additional information is needed for better clarifying the issue. thanks in advance.