This question already has answers here:
Firebase Query Double Nested
(3 answers)
Closed 10 months ago.
I have a child that get created inside my RTDB in firebase. This happens by a webservice that sends this data to firebase via the Rest API. It happens every 10 min. The data arrives and the function picks it up when it occurs. The node hold a couple of values but specifically a device: "3523EB" value that's needed for the next part. Below is the structure of that node data:
The 'device' value here "3523EB" need to update a different table that it needs to go look for with and equalTo in the devices table. Problem is the devices table has the userID and the key. So its 2 un-known values that it needs to search through as the chils its looking for contains imei:"3523EB" and it's only that child that needs to be updated.
I've tried using ref.orderByChild("imei").equalTo(deviceRef).on("child_added", function(snapshot) {
So device:"3523EB" in sigfox table has to look for and update only the child with this in is data as imei:"3523EB" in the devices table, but the {UserId} and the {key} where the table resides in devices is unknow.
Getting the values from sigfox in the function as it hits the DB is no issue: to take the value and look for the child in devices then update that child proves to be a challenge.
exports.UpdateDeviceData = functions.database.ref('sigfox/{key}/')
.onCreate((snapshot, context) => {
const deviceRef = snapshot.val()['device'];
const batteryRef = snapshot.val()['battery'];
const speedRef = snapshot.val()['speed'];
const temperatureRef = snapshot.val()['temperature'];
const latitudeRef = snapshot.val()['latitude'];
const longitudeRef = snapshot.val()['longitude'];
const timeRef = snapshot.val()['time'];
const now = new Date().getTime();
functions.logger.log('Sigfox Data Updated for device: ', deviceRef);
var db = admin.database();
var refi = db.ref("devices/");
refi.once("value", function(snapshot) {
snapshot.forEach(function(child) {
var key = child.key;
snapshot.ref.update({
battery: batteryRef,
speed: speedRef,
temperature: temperatureRef,
lati: latitudeRef,
longi: longitudeRef,
updateTime: timeRef })
functions.logger.log('Device Key Details: ',child.val());
})
});
return null;
});
Please help...
Restuctured and it works perfectly fine now:
var db = admin.database();
var query = db.ref('devices/' + deviceRef).orderByChild("imei").equalTo(deviceRef);
query.on("child_added", function (snapshot) {
snapshot.ref.update({
battery: batteryRef,
speed: speedRef,
temperature: temperatureRef,
lati: latitudeRef,
longi: longitudeRef,
updated: now})
});
thanks to: Frank van Puffelen
Related
This is the database structure i have i want to get logged in user data.
i want to make table of data: Columns: Date,Status
Also i want to make percentage piechart wheel by calculating success and failure rate. but not able to get data from firebase.
I tried this but not working. I'm able to log in log out successfully. I'm also able to add data in firebase only once per date.
I'm just not able to fetch and show in table.
Here's what i tried:
`
// Get the user's attendance records
firebase.database().ref("attendance").once("value", function(snapshot) {
// Get the attendance data
var attendanceData = snapshot.val();
var userId = firebase.auth().currentUser.uid;
// Display the attendance history
for (var email in attendanceData) {
var attendance = attendanceData[email][userId];
if (attendance) {
for (var date in attendance) {
var status = attendance[date].status;
var tr = document.createElement("tr");
tr.innerHTML = `<td>${date}</td><td>${status}</td>`;
attendanceHistoryTable.appendChild(tr);
}
}
}
});
If I understand correctly, you have a data structure like this:
attendance: {
user: {
"$uid": {
"$date": {
Status: "..."
}
}
}
}
And from this you want to show the status per date for the current user.
If that's indeed the use-case, you can do this with:
const userId = firebase.auth().currentUser.uid;
const attendanceRef = firebase.database().ref("attendance");
const userRef = attendanceRef.child("users").child(userId);
userRef.once("value", function(userSnapshot) {
userSnapshot.forEach((dateSnapshot) => {
const status = dateSnapshot.child("Status").val();
console.log(`User: ${userSnapshot.key}, Date: ${dateSnapshot.key}, Status: ${status}`);
... // TODO: add the data to the HTML as you're already doing
});
});
The main changes I made here:
This only loads the data for the current user, instead of for all users.
This code uses the built-in forEach operation of a DataSnapshot.
This code gives more meaningful names to the variables, so that it's easier to parse what is going on.
This code uses "Status" rather then status, since that's the key in your database screenshot too.
I'm on a vuejs and firebase project.
And I would like to display only the data recorded today.
But I don't know how to do it.
Can you please help me.
created() {
const dfRef = db.ref("Colis Requetes")
dfRef.orderByChild("created_at").on("value", (dataSnapshot) => {
const itemsArray = [];
dataSnapshot.forEach((childSnapshot) => {
const childData = childSnapshot.val();
const childDataKey = childSnapshot.key;
this.Colis = childData;
itemsArray.push({
id: childDataKey,
client_name: childData.client_name,
client_phone: childData.client_phone,
colis_type: childData.colis_type,
payment_method: childData.payment_method,
});
});
this.Colis = itemsArray.reverse();
});
}
The orderByChild property is for displaying data from the most recent date to the oldest date. However, I want only the recordings made today to appear on the page.
I'm on a vuejs and firebase project.
And I would like to display only the data recorded today.
But I don't know how to do it.
Can you please help me.
You'll want to use a filtering operation for that, most likely startAt.
If your created_at contains a timestamp (going forward, please include such information in your question), that'd be something like:
var start = new Date();
start.setUTCHours(0,0,0,0); // based on https://stackoverflow.com/a/8636674
const dfRef = db.ref("Colis Requetes")
dfRef
.orderByChild("created_at")
.startAt(start.getTime()) // 👈
.on("value", (dataSnapshot) => {
...
Im trying to update two children in my database (using realtime database firebase), but when database is updated, my application go back to the home screen for no reason.
When I update only in "Tasks" it works (the app doesnt go back to the home screen) but when I combine update in "Tasks" and "Users" there is this problem..
Maybe i dont do it the good way.. Any ideas?
statusPlayback = async (status) => {
const { navigation } = this.props
const task = navigation.getParam('task')
//console.log("task = ", task);
//to check if we arrived to the end of the
if (status.didJustFinish) {
const CountVideoRef = firebase
.database()
.ref("Tasks")
.child(firebase.auth().currentUser.uid).child(task.AssignPerson)
.child(task.taskname);
CountVideoRef.once("value").then((snapshot) => {
CountVideoRef.update({
countViewVideo: snapshot.val().countViewVideo + 1,
});
})
const PointEndVideoRef = firebase
.database()
.ref("Users")
.child(firebase.auth().currentUser.uid);
PointEndVideoRef.once("value").then((snapshot1) => {
PointEndVideoRef.update({
Points: snapshot1.val().Points + 10,
});
const points = (snapshot1.val().Points) + 10
//console.log("points = ", points)
this.props.updatePoints({ points: points })
})
this.setState({ showButtonVisible: true });
}
};
I doubt this is the cause of the navigation problem, but this style of updating is fraught with problems:
CountVideoRef.once("value").then((snapshot) => {
CountVideoRef.update({
countViewVideo: snapshot.val().countViewVideo + 1,
});
})
If you need to update an existing value in the database based on its existing value, you should use a transaction to prevent race conditions when multiple users perform the same action around the same time (or while offline).
In this case, you can use the simpler atomic server-side increment operation, which means the above becomes:
CountVideoRef.set(firebase.database.ServerValue.increment(1));
Here is my firebase database:
All I am trying to do is add data to this child(-M0h3ipBGdzHBuH129WT) without overwriting anything. Is there a way to do so?
Currently the code below creates a todolist,
const task = document.getElementById('task');
const userId = document.getElementById('userId');
const childreference = document.getElementById('childreference').value
const addtodolistcontent = document.getElementById('addtodolistcontent')
const database = firebase.database();
const usersRef = database.ref('todolist_users');
createtodolist.addEventListener('click', e => {
const usersRef = database.ref('todolist_users');
e.preventDefault();
const autoId = usersRef.push().key
usersRef.child(autoId).set({
userId: userId.value,
task: task.value
})
});
And the second code snippet, I am trying to reference that unique ID and add to it.
addtodolistbtn.addEventListener('click', e => {
e.preventDefault();
firebase.database().ref('todolist_users/' + childreference).push({
task2: addtodolistcontent.value
}) });
Another problem i have just thought is that if i can get this to work, i need another way of making a task 3,4,5 and so on.
Is this a complicated way of making a simple todo list where you can add and delete as you please? If so how else can i make it?
push() method will create a new child everytime you add, if you want to add it under the unique userId, then you need to use the following:
firebase.database().ref('todolist_users/' + childreference).child(userId).push({
task2: addtodolistcontent.value
})
https://firebase.google.com/docs/reference/js/firebase.database.Reference#set
This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 3 years ago.
My google cloud function is triggered when an update takes place on a document in my firestore database. The update would happen from a string being added/removed to an array in the database. How do I get the exact value added/removed to the database in the cloud function?
// Update event attendance
exports.updateEventAttendance = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
const newValue = change.after.data();
const oldValue = change.before.data();
const newEvents = newValue.eventsAttended;
const oldEvents = oldValue.eventsAttended;
// We'll only update if the eventsAttended has changed.
// This is crucial to prevent infinite loops.
if (newEvents === oldEvents) return null;
const newCount = newEvents.length;
const oldCount = oldEvents.length;
var db = admin.firestore()
if (newCount > oldCount) {
// Event added
// Get event id
// GET STRING THAT WAS ADDED TO THE DATABASE AND DO SOMETHING WITH IT
} else if (oldCount > newCount) {
// Event removed
// Get event id
// GET STRING THAT WAS REMOVED FROM DATABASE AND DO SOMETHING WITH IT
}
return null;
});
Cloud Functions tells you the document state before and after the write operation within its change.before and change.after parameters. It doesn't specify what specific fields were changed within the document, so you will have to determine that yourself by comparing the relevant fields in the before and after snapshots.