Node redis get () synchronous help me

, notEnterUsers: function(){
      var users;
      this.store.get("users",function(err,result){    
        var result = JSON.parse(result);
         users = result.filter(function(element, index, arr){
          return (element.status === 0);
        });
      });

      console.log(users);
      return users;
    }

How to solve this problem user = undefinded?

rule 1 ....

must be a user-user object

+4
source share
1 answer

With Node.js, you should use the Continue Transfer Style (CPS). This example cannot work because the callback specified as the get parameter has not yet completed when "console.log (users)" is executed.

The notEnterUsers function must have a callback parameter and call it after filling in the user variable. Sort of:

, notEnterUsers: function(callback){
      var users;
      this.store.get("users",function(err,result){    
        var result = JSON.parse(result);
        users = result.filter(function(element, index, arr){
          return (element.status === 0);
        });
        callback(users);
      });
    }

Node.js is fundamentally asynchronous, and your scripts should try to hug it, and not try to simulate synchronous behavior.

0
source

Source: https://habr.com/ru/post/1526287/


All Articles