const firebase = require("...")
const db = firebase.firestore()
const DB_COLLECTION_NAME = db.collection("COLLECTION_NAME")
const newData = {
id: Date.now().toString(),
name: "sam",
phone: "910000000000"
}
DB_COLLECTION_NAME.set(newData).then((res) => { console.log(res) }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.get().then((snap) => { snap.forEach((doc) => { console.log(doc); }) }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.doc(DOC_ID).get().then((doc) => { console.log(doc); }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.where("name", "==", "sam").get().then((snap) => { snap.forEach((doc) => { console.log(doc); }) }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.orderBy("date", "desc").get().then((snap) => { snap.forEach((doc) => { console.log(doc); }) }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.orderBy("date", "asc").get().then((snap) => { snap.forEach((doc) => { console.log(doc); }) }).catch((err) => { console.log(err); })
const updateData = {
name: "john",
}
DB_COLLECTION_NAME.doc(DOC_ID).update(updateData).then((res) => { console.log(res) }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.doc(DOC_ID).delete().then((doc) => { console.log(doc); }).catch((err) => { console.log(err); })
#FIREBASE REALTIME
const db = firebase.database()
const DB_COLLECTION_NAME = db.ref("COLLECTION_NAME")
const newData = {
id: Date.now().toString(),
name: "sam",
phone: "910000000000"
}
DB_COLLECTION_NAME.set(newData).then((res) => { console.log(res) }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.push(newData).then((res) => { console.log(res) }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.on('value', (snapshot) => { console.log(snapshot.val()); }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.once('value', (snapshot) => { console.log(snapshot.val()); }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.child(DOC_ID).get().then((snapshot) => { console.log(snapshot.val()); }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.where("name", "==", "sam").get().then((snap) => { snap.forEach((doc) => { console.log(doc); }) }).catch((err) => { console.log(err); })
const updateData = {
name: "john",
}
DB_COLLECTION_NAME.child(DOC_ID).update(updateData).then((snap) => { console.log(snap) }).catch((err) => { console.log(err); })
DB_COLLECTION_NAME.doc(DOC_ID).delete().then((snap) => { console.log(snap); }).catch((err) => { console.log(err); })
async function uploadImage(file) {
const image = new Promise((resolve, reject) => {
var path = `file/${userid}/`
var fileName = `${Date.now().toString()}.${extention}`
var storageRef = firebase.storage().ref(path + fileName);
storageRef.put(file).then(function (snapshot) {
console.log('Uploaded a blob or file!');
storageRef.getDownloadURL().then(function (url) {
resolve(url)
})
});
});
const imageURL=await image
return imageURL
}