最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Array.prototype.find() returns undefined in async function - Stack Overflow

programmeradmin0浏览0评论

I have an issue with the Array.prototype.find() method. It doesn't seem to work properly when it is used in $http request.. I've tried everything that came to mind but it always returns undefined. I guess the bug is there because I logged response.data and user_to_find before defining found_user and their values are as expected.

So I have this service:

app.service('UserService', ['$http','$q', function($http, $q) {
  return {
    findUser: function(user_to_find) {
      let defer = $q.defer();
      $http({
        method: "GET",
        url: 'http://localhost:3000/users'
      }).then(function(response){
        let found_user = response.data.find(acc=>{ //found_user is always undefined
          acc.username == user_to_find;
        })
        defer.resolve(found_user);
      }),function(response) {
        defer.reject(response);
        console.log("Error finding user");
      }
      return defer.promise;
    }
  }
}])

I have an issue with the Array.prototype.find() method. It doesn't seem to work properly when it is used in $http request.. I've tried everything that came to mind but it always returns undefined. I guess the bug is there because I logged response.data and user_to_find before defining found_user and their values are as expected.

So I have this service:

app.service('UserService', ['$http','$q', function($http, $q) {
  return {
    findUser: function(user_to_find) {
      let defer = $q.defer();
      $http({
        method: "GET",
        url: 'http://localhost:3000/users'
      }).then(function(response){
        let found_user = response.data.find(acc=>{ //found_user is always undefined
          acc.username == user_to_find;
        })
        defer.resolve(found_user);
      }),function(response) {
        defer.reject(response);
        console.log("Error finding user");
      }
      return defer.promise;
    }
  }
}])
Share Improve this question edited Jun 12, 2018 at 12:59 Hasan Gholamali 6334 silver badges13 bronze badges asked Jun 12, 2018 at 12:36 AlexxarAlexxar 611 gold badge1 silver badge4 bronze badges 2
  • Have you looked at the debugger? Is the username case right? Try using a function argument in find instead, could be easier to debug – Ares Commented Jun 12, 2018 at 12:41
  • 2 Avoid the deferred antipattern! – Bergi Commented Jun 12, 2018 at 12:41
Add a comment  | 

1 Answer 1

Reset to default 17

Your are missing a return. The predicate (the function passed to find) returns undefined in your case, so the find algorithm does not get a "match".

Just add the return:

    let found_user = response.data.find(acc=>{
      return acc.username == user_to_find;
    })

You can leave it out, if you are not using curly braces

    let found_user = response.data.find(acc=> (
      acc.username === user_to_find;
    ))

I'd recommend also to use === (strict comparison) instead of == for comparison.

发布评论

评论列表(0)

  1. 暂无评论