const ref = firebase.database().ref('fruits')
const fruitsRef = firebase.database().ref('fruits/')
fruitsRef.set({
apple: {
price: 1.50
},
banana: {
price: 2.50
}
})
const fruitsRef = firebase.database().ref('fruits/apple')
fruitsRef.update({
price: 2.25
})
This will generate a unique id for each items pushed.
const fruitsRef = firebase.database().ref('fruits')
fruitsRef.push({
price: 1.5,
name: 'apple'
})
fruitsRef.push({
price: 2.5,
name: 'banana'
})
const fruitsRef = firebase.database().ref('fruits')
const keys = fruitsRef.key()
const fruitsRef = firebase.database()
const bananaRef = fruitsRef.child('fruits').child('-KGb1Ls-gEErWbAMMnZC').child('price')
bananaRef.transaction(function (price) {
return price * 1.05
})
const ref = firebase.database().ref()
ref.on('value', (snapshot) => {
console.log(snapshot.val())
}, function (error) {
console.log(error)
})
const fruitsRef = firebase.database().ref('fruits')
fruitsRef.on('child_added', (data, prevChildKey) => {
const newFruit = data.val()
console.log(newFruit.name)
console.log(newFruit.price)
console.log(prevChildKey)
})
fruitsRef.on('child_changed', (data) => {
const fruit = data.val()
console.log(fruit)
})
fruitsRef.on('child_removed', (data) => {
const fruit = data.val()
console.log(fruit)
})
fruitsRef.on('value', (data) => {
console.log(data.val())
}, (error) => {
console.log(error)
})
// Remove all callbacks with the value event type
fruitsRef.off('value')
// Remove all callbacks
fruitsRef.off()
fruitsRef.orderByChild('name').on('child_added', (data) => {
console.log(data.val().name)
})
fruitsRef.orderByKey().on('child_added', (data) => {
console.log(data.key)
})
fruitsRef.orderByValue().on('value', (data) => {
data.forEach((data) => {
console.log(data.key, data.val())
})
})
limitToFirst method returns the specified number of items beginning from the first one
const firstFruitRef = firebase.database().ref('fruits').limitToFirst(1)
firstFruitRef.on('value', (data) => {
console.log(data.val())
}, (error) => {
console.log(error)
})
limitToLast method returns a specified number of items beginning from the last one.
const lastFruitRef = firebase.database().ref('fruits').limitToLast(1)
lastFruitRef.on('value', (data) => {
console.log(data.val())
}, (error) => {
console.log(error)
})
const fruitsRef = firebase.database().ref('fruits')
fruitsRef.orderByChild('name').startAt('banana').on('child_added', (data) => {
console.log('start at filter:', data.val().name)
})
fruitsRef.orderBychild('name').endAt('banana').on('child_added', (data) => {
console.log('end at:', data.val().name)
})
fruitsRef.orderByChild('name').equalTo('banana').on('child_added', (data) => {
console.log('equal to filter:', data.val().name)
})
fruitsRef.orderByChild('price').startAt(10).on('child_added', (data) => {
console.log('age filter:', data.val().name)
})
const connectedRef = firebase.database().ref('.info/connected')
connectedRef.on('value', (snapshot) => {
if (snapshot.val() === true) {
console.log('connected')
} else {
console.log('not connected')
}
})