简体   繁体   中英

How to do a condition check and return a custom object when using a Promise?

I have created a provider that will handle all firebase db related calls. If the method getUser is written as foll. then it returns the object (note that .once is a promise).

    getUser()
          {
            return firebase.database().ref(`/users/`)
            .once('value', dataSnapshot => {dataSnapshot.val();
            });
          }

However, if the object is null, then it returns a null and I have to handle this at the page level as follows:

ngOnInit() {         
    this.dbcon.getUser().then(dbvalue=>{
      if(dbvalue.val()){
     //..do soemthing.. 
     }      
    }); 

Ideally I would like the provider to either return a valid object or something like {sorcode:''}, but not null. Any advise to do this at the provider level will be helpful.

I tried implementing this at the provider level and was expecting it to return hard coded object {sorcode:''}. But it just returns null.

Code on page:

  ngOnInit() {         
    this.dbcon.getUser().then(dbvalue=>{
      console.log(dbvalue.val());
    });       
  }

Code in provider (note that .once is a promise):

   getUser()
  {
    return firebase.database().ref(`/users/`).once('value', dataSnapshot => {
           if (dataSnapshot.val()==null)
           {
              {sorcode:''};
           }
           else
           {
              dataSnapshot.val();
           }
    });
  }

If I use the 'return' keyword within the IF loop, and not before calling the promise (as shown below), then it gives an error on the page as: "Cannot read property 'then' of undefined"

Code in provider:

   getUser()
  {
    firebase.database().ref(`/users/`).once('value', dataSnapshot => {
           if (dataSnapshot.val()==null)
           {
              return {sorcode:''};
           }
           else
           {
              return dataSnapshot.val();
           }
    });
  }

If I use the 'return' keyword within the IF loop, and also before calling the promise (as shown below), then it still gives the original issue - returns null when the object does not exist.

Code in provider:

       getUser()
      {
               return firebase.database().ref(`/users/`).once('value', dataSnapshot => {
var x=dataSnapshot.val();
               if (x==null)
               {
                  return {sorcode:''};
               }
               else
               {
                  return dataSnapshot.val();
               }
        });
      }

The are return statements missing.

either:

return firebase.database().ref(`/users/`).once('value', dataSnapshot => {
       if (dataSnapshot.val()==null)
       {
          return {sorcode:''};
       }
       else
       {
          return dataSnapshot.val();
       }
});

or

 firebase.database().ref(`/users/`).once('value', dataSnapshot =>
   dataSnapshot.val() == null ? 
     {sorcode:''} :
     dataSnapshot.val()
 );

UPDATE

From here »returns firebase.Promise[…]«. So it should probably be (?):

firebase.database().ref(`/users/`).once('value').then(dataSnapshot => <return stuff here>);

As it seams, the second argument is a »success callback«

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM