What is the best way to retrieve the previous object value (on value) in Firebase?

Here's a sample code snippet:

Firebase Database
   name: Sonic

when changed value
   name: Sonita


FirebaseRef.on('value', function(snapshot) {
  var previousName = ?; //Sonic
  var newName = snapshot.val(); //Sonita
});

Answer №1

If you're looking to access the previous value of a node when it changes, Firebase does not offer a direct solution for this. You will need to store the previous value somewhere yourself.

One approach is to save the previous value as a child node. Here's how you can do it:

1) When updating the value:

var new_name = "Sonich";
FireBaseRef.once('value', function(snapshot) {
  var prev_name= snapshot.val(); // Previous value
  FireBaseRef.set({
    "value": new_name,
    "prev_value": prev_name
  });
});

2) To retrieve the values:

FireBaseRef.on('value', function(snapshot) {
  snapshot = snapshot.val();
  var previous_name = snapshot.prev_value;
  var new_name = snapshot.value;
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm currently experiencing a challenge with a project I'm tackling that specifically deals with chart.js

In my recent coding project, I created a script to gather user input and then present it in various chart formats. However, upon executing the code, the selected chart fails to display after inputting values and clicking on the "generate chart" button. Her ...

Fill the dropdown menu with options from a collection and automatically adjust the list based on the user's selection in the meteor

As a newcomer to Meteor, I am facing an issue with populating a select box from a MongoDB collection. Despite my attempts, the solution I tried below did not work as expected: <template name="clist"> <div class="input-field"> <select> ...

Specify the return type based on specific parameter value

I'm facing a situation where I have two definitions that are identical, but I need them to behave differently based on the value of the limit parameter. Specifically, I want the first definition to return Promise<Cursor<T>> when limit is g ...

What steps can be taken to ensure a protractor test fails when a function does not succeed

For debugging my protractor test cases, I have developed helper methods. One of the key functions is waiting for an element to be clickable. To ensure that Protractor has enough time to find and enable the element, I am implementing a loop. However, in cas ...

What is the method for selectively applying a "fade to black" mask to an input field?

For my current project, I need to incorporate a design feature where a "fade to black" mask gradually appears on the left side of an input field once the text content reaches the maximum visible width of the input box. The image below provides a visual re ...

I built a custom Angular application integrated with Firebase, and I'm looking for a way to allow every user to have their individual table for managing tasks

I am looking to enhance my code so that each user who is logged in has their own tasks table, which they can update and delete. Additionally, I need guidance on how to hide menu items tasks, add-tasks, logout for users who are not logged in, and display th ...

Retrieve information from the database when the tab key is activated

Hey guys! I need some help with my project. I'm using jquery, ajax, and php to pull data from a database. My goal is to have the data automatically filled in a textarea when a user inputs a product code and presses the TAB key. I'm still new to a ...

Angular 5 using AngularFire for Seamless Firebase Login Experience

Utilizing AngularFire2 in combination with Firebase and Angular 5, I have successfully implemented a login system. However, upon refreshing the page, a slight flicker occurs due to the delay in loading JavaScript. This issue is unfamiliar to me as I'm ...

Attempting to add an element using jQuery

After a user selects an option from my dropdown list, I want to create a new element. However, the result I'm getting is not what I expected. The current outcome looks like this: As you can see, the link 'remove' gets incremented every time ...

Attempting to implement a JavaScript function that will trigger a MySQL query for a defined period of time when a user clicks in PHP

After implementing the code to run a JavaScript function every 5 seconds when the user clicks the claim button, it appears that the script is not working as expected. As I am not proficient in JavaScript and only have knowledge of PHP and MySQL, I am strug ...

Customizing button appearances in the control div on Google Maps - A guide

Is there a way to manipulate elements and adjust styles by clicking buttons on the map control div? I want to achieve the same effect on the map as with the buttons in the table. Code: jsFiddle Update: Thanks to your assistance, I was able to achieve th ...

Best practices for securing passwords using Chrome DevTools in React development

React developer tool inspector Is there a way to prevent password values from appearing in the inspector as a state when handling form submissions in ReactJS, especially when using Chrome's React developer tool? ...

When utilizing Angular, be cautious of encountering an "undefined" error when attempting to add a JavaScript function

I have a good understanding of Javascript and Jquery, but I am relatively new to Angular. Although I've used jquery with angular in the past without any issues, the application I recently inherited is causing me quite a bit of trouble. Whenever I cli ...

What are some solutions for troubleshooting a rapid-moving Selenium web driver?

My code is not functioning correctly and I need a better solution. The fast speed of execution makes it difficult for me to detect where the issue lies. Below is my current code: public static void main(String[] args) { //**************************** ...

Cordova does not support the transform property

In the process of creating an Apache Cordova app using the Ionic framework and working with Phonegap Build, I encountered a challenge. The main page of my app features rows of icons that appear like this: https://i.sstatic.net/7LE7r.png However, there is ...

SPRING --- Tips for sending an array of objects to a controller in Java framework

Can someone help me with this issue? I am using AngularJS to submit data to my Spring controller: @RequestParam(value = "hashtag[]") hashtag[] o The above code works for array parameters but not for an array object. This is my JavaScript script: $http ...

The Process of Sending Values from app.js to a Vue File in Vue.js

My app.js is currently receiving a value called gtotal. I am trying to pass this value to the orderForm.vue file but am facing some difficulties in achieving this. require('./bootstrap'); window.Vue = require('vue'); window.EventBus ...

Creating personalized text in the react-native calendar界 can be as simple as following these

I am currently using the react-native-calendars library in my react-native project image description goes here I want to display text or emojis for each day if they exist import React from 'react'; import { Text, View, StyleSheet, TextInput } fr ...

Exploring the use of functions in the setup method of Vue 3

I'm currently working on a simple app and utilizing mock-json-server to mimic http requests. Within my project, I have defined a function designed to retrieve the necessary information: import { ref } from 'vue' const getScores = () => ...

Navigate through the Jquery slider by continuously scrolling to the right or simply clicking

Is there a way to prevent my slider from continuously scrolling? I think it has something to do with the offset parameter but I'm having trouble figuring it out. Any assistance would be greatly appreciated. var $container = $(container); var resizeF ...