angularjsionic-frameworkbackand

How to give variable to filter in object in backand using ionic and angularjs


I need to pass variable inside the filter of value field of object in services.

Here is my code:

.service('LoginService', function (Backand) {

var email="";

  service.getUsername = function(){
    email=Backand.user.getUsername();
    return email; 
};

service.getUserData=function(){
   return Backand.object.getList("users", {
    "pageSize": 20,
    "pageNumber": 1,
    "filter": [
      {
        "fieldName": "email",
        "operator": "equals",
        "value":email
      }
            ],
   "sort": []
      })
  };
)}

In the above code I store email in email variable and I want to pass it in value field in service.getUserData() but it cannot take it. How can I figure it out?


Solution

  • As devqon mentioned, make sure the variable "email" actually has value when this call is made. The Backand SDK returns a promise for all method calls, meaning you'll need to update your code above with a handler for the success/failure of the GetUsername() function, as follows:

    Backand.user.getUsername().then(function(data){email = data.email;});
    

    This defines a simple success handler that will populate the email variable with the result of the call. You should then be able to pass it back to the API without issue, but you'll likely need to restructure your code to account for the asynchronous nature of the getUsername call - returning the result of GetUsername() in response to a call to this method won't work until after the promise is resolved, and at the moment you're only returning the promise itself. There is more info in our documentation at http://docs.backand.com/?javascript#sdk-methods