2

I'm trying to send data to firebase. The data is saved to the database after the function is performed, how can I overwrite it during the function execution?

import firebase from 'firebase';

  var config = {
    apiKey: "xxx",
    authDomain: "xxx.firebaseapp.com",
    databaseURL: "https://xxx.firebaseio.com",
    projectId: "xxx",
    storageBucket: "xxx.appspot.com",
    messagingSenderId: "xxx"
  };
  firebase.initializeApp(config);

var db = firebase.database();
var sleep = require('sleep');
function run(TIME) {
  db.ref('/test/').child('status').set('1');
  sleep.sleep(TIME);
  db.ref('/test/').child('status').set('2');
  sleep.sleep(TIME);
  db.ref('/test/').child('status').set('3');
  sleep.sleep(TIME);
  db.ref('/test/').child('status').set('4');

};
//========================================<<<< Now I see status in Firebase
run(5);
2
  • Could you please explain in more detail what you try to achieve? Thx Commented Sep 7, 2018 at 14:03
  • I want to have status in firebase. WORKING - before starting function and DONE after. Commented Sep 7, 2018 at 14:51

1 Answer 1

1

The set() method is asynchronous and returns a promise that resolves when write to server is complete, as explained in the doc here.

From you comment above I understand you want to have a status "WORKING - before starting function and DONE after".

So you should do something along the following lines:

var status = '';
var adaNameRef = firebase.database().ref('users/ada/name');
status = 'WORKING';
adaNameRef.set({ first: 'Ada', last: 'Lovelace' })
  .then(function() {
    status = 'DONE'; 
  })
  .catch(function(error) {
    console.log('Synchronization failed');
  });

If you want to "write multiple values to the Database at once", you should use the update() method. See here and here.

Similarly to the set() method, the update() method is asynchronous and returns a promise that resolves when write to server is complete, so you would use the same logic to update the value of status


UPDATE following your comment

1. Send status 'WORKING' to FB 2. Set Relay to ON 3. Wait x seconds 4. Send status 'DONE' to FB 5. Set Relay to OFF

If I understood correctly, this should work (not tested however):

var adaNameRef = firebase.database().ref('users/ada/name');
adaNameRef.set({ status: 'WORKING'})
  .then(function() {
    // Set Relay to ON  ... don't know exactly how you "set the relay"
    sleep.sleep(x);
    return adaNameRef.set({ status: 'DONE'})
  })
  .then(function() {
    // Set Relay to OFF 
  })
  .catch(function(error) {
    console.log(error);
  });
Sign up to request clarification or add additional context in comments.

3 Comments

Renaud, thank you for help, I want send status to firebase.
@Tom Not 100% sure what you want to do but by mixing sleep and asynchronous methods triggered in // you will probably get strange results. If you explain really in detail what you want to do we may help further.
I want: 1. Send status 'WORKING' to FB 2. Set Relay to ON 3. Wait x seconds 4. Send status 'DONE' to FB 5. Set Relay to OFF Thank you @Renaud

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.