Node.js app reading chached json value

I have recently started making a node.js app and have gotten to a point at which I need to read a value of a JSON that has just been updated, but not successfully, I have tried using the ‘sleep’ module to stop threads for a small delay but still no luck.

I haven’t been using node.js for very long either, for only about 5 months.

-Thank you in advance


Source: stackoverflow-javascript

React-native and Firebase: Wait for Firebase data

I am currently implementing an application which relies on Firebase data before it can continue. However, I often (unless I deliberately wait) get the result Cannot read property [property] of null. I very much suspect that this is because the firebase request could not be made before I call this object.

Now I am looking for methods to implement a rendez-vous, or a barrier, really anything that implements a checkpoint where all firebase data are received, before it continues. Is there anything in Javascript which helps me to do so, or any library, or any react-native library that helps me to achieve this?

My code looks as follows: (Fb is the firebase interface)

@action
  bookInterestedBike() {
    this.bookedBikeNo = this.interestBikeNo;
    this.downloadBikeObj();
    this.updateBikeDataStartRide();
    this.updateUserDataStartRide();
    //
    //This is where the firebase checkpoint / barrier should happen, and no progress should be made unless this data was fetched!
    //
    this.startTimer();
  }

@action
  downloadBikeObj() {
    Fb.staticBikes.child(String(this.bookedBikeNo)).once('value', (bikeObj) => {
      this.bikeObj = bikeObj.val();
      console.log("Save object is: ");
      console.log(this.bikeObj);
    });
  }

  updateBikeDataStartRide() {
    var updateVals = {}
    updateVals[this.interestBikeNo] = {
      bike_no: this.interestBikeNo,
      current_user: "self",
      positionLat: this.usrLat,
      positionLng: this.usrLng
    };
    Fb.bikes.update(updateVals);
    return false;
  };

  updateUserDataStartRide() {
    var updateVals = {}
    updateVals[this.uuid] = {
      bike_no: this.bookedBikeNo,
      uuid: this.uuid //TODO: remove this in deployment
    };
    Fb.users.update(updateVals);
    return false;
  };


Source: stackoverflow-javascript

JS continue after multiple .load()-functions done

I’m having a problem with an asynchronous JS function. Well I have a function which inserts elements one by one. This works. But I would like to continue after all .load()-functions are done.

The way I’ve implemented the Deferred() part is obviously wrong but I have no clue how to achieve this.

Note: I have simplified the code for a better clarity.

var d = new jQuery.Deferred();
var i = index;
function loadView() {
    if(i > -1 && i < array.length) {
        if(!jQuery.contains(document.documentElement, $(selector)[0])) {
            /* do stuff here */
            panel.load(myPHPfile, function() {
                $dom.append($(selector));
                i++;
                if(i < array.length) {
                    loadView();
                    d.resolve();
                }
            });
        }
    }
}
loadView();

jQuery.when(d).then(function() {
    /* when all elements are insert, do something */
});

I am thankful for any help!


Source: stackoverflow-javascript

firebase async – cant access object data

$('#btn_searchUser').click(function(){
  var emailSearch = $('#txt_searchEmail').val();
  var rootRef = firebase.database().ref('users/');

  rootRef.orderByChild('email').equalTo(emailSearch).once('value').then(function(snapshot){

   displayUserDetails(snapshot);
 }, function(error){
   console.log(error);
 });
});
function displayUserDetails(snapshot) {
var data = snapshot.val();
console.log(data);
}

logging the data logs the object as expected… but data.name or any other method ive tried logs undefined!

Very new to JS so any help is appreciated. Thanks


Source: stackoverflow-javascript

callback within a function which should return to another function

Working in node.js and trying to get node to run python-shell, calculate a value and then return the answer to computePython() as an object with key result.

How do I get message to return to result: FutureValue() ?
console.log(message) works within .on(‘message’..

function computePython(){
  return {
  result: FutureValue() // I want message from FutureValue() to show up here
  };
}

function FutureValue(){
  var rate = 0.05;
  var nper = 10;
  var pmt = 100;
  var pv = 100;
  var result;

  new PythonShell('future_value.py', jsc(options, {
    args: [rate, nper, pmt, pv]
    }))
    .on('message', function (message) {
       console.log(message); //works
       return message;
    });
  }


Source: stackoverflow-javascript

Seeking clarification about asynchronous javascript

I am a beginning javascript programmer. I have been trying to understand asynchronous javascript but I wanted to clear up a couple of things.

I understand that javascript runs in a single thread and that you can use callback functions to make your code asynchronous, but I am confused about what makes a callback function asynchronous or not.

A lot of the async callbacks seem to follow a pattern where a function has as its parameters a certain action and then a callback function which is to execute when that action is complete:

jQuery.get('page.html', function (data) {
    console.log("second");
});
console.log('first');

What is it specifically that makes the callback in the parameter here execute at a later time? Is it that the get method here is pre-defined as some sort of a special method (because it fetches a file) that if you pass a function as a second parameter, it behaves in a asynchronous manner?

How can you make functions that you would write yourself asynchronous?

Thanks


Source: stackoverflow-javascript