AngularJS check if promise is empty or not

2019-09-21 10:48发布

问题:

Hi I have this code for the purpose of checking if there are users in the database, if it finds shows a list of users on a view, otherwise shows a view with form of user creation, but didn't work the check expression what I'm doing wrong

users = Users.query();

users.$promise.then(
    function (data) {
        if (!data) {
            $location.url('/NewUser')
        } else {
            $location.url('/UsersList')
        }

    });

回答1:

It is probably returning an empty array in case nothing is found. Try checking the data length.

if (data.length == 0) {
   $location.url('/NewUser')
} else {
   $location.url('/UsersList')
}