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

javascript - "Cannot read property 'id' of undefined" when parsing an array declared in a fact

programmeradmin0浏览0评论

I am building a basic app (with MEAN web frameworks and node webkit) in order to understand angularjs better.

Here is the content of my notificationFactory.js

function notificationFactory() {

    var fooMessages = [

        {
            id: 4,
            dismissable: true,
            name: "fooooBaaar",
            function: '',
            showInTopBar: false,
            priority: "high",
            icon: 'fooIconBarBarBar',
            topBarIcon: 'fooIconIconIcon'
        },

        {
            id: 3,
            dismissable: true,
            name: "foofooBarBar",
            function: '',
            showInTopBar: false,
            priority: "high",
            icon: 'fooIconfooIcon',
            topBarIcon: 'IconIconIcon'
        },

        {
            id: 2,
            dismissable: true,
            name: "foo foo",
            function: '',
            showInTopBar: false,
            priority: "high",
            icon: 'fooBaaaaaar',
            topBarIcon: 'IconFooIcon'
        },

        {
            id: 1,
            dismissable: true,
            name: "foo",
            function: '',
            showInTopBar: false,
            priority: "high",
            icon: 'fooIcon',
            topBarIcon: 'fooIconIcon'
        },
    ]

    fooMessages.TopBarDismiss = function (message) {
        $.each(fooMessages, function (i, v) {
            if(v.id = message.id) {
                fooMessages.splice(i,1);
            }
        });

    }

    return fooMessages;

}

angular.module('fooDemo').factory('notificationFactory', notificationFactory);

I call the TopBarDismiss() function in an HTML template using:

<div class="fooDismiss" ng-click="notificationFactory.TopBarDismiss(message)">Dismiss</div>

When I check out the console after pressing my Dismiss "button", I get this:

 TypeError: Cannot read property 'id' of undefined
at notificationFactory.js:94
at Function.m.extend.each (jquery.min.js:2)
at Array.fooMessages.TopBarDismiss (notificationFactory.js:93)
at fb.functionCall (angular.js:10847)
at angular-touch.js:472
at k.$get.k.$eval (angular.js:12702)
at k.$get.k.$apply (angular.js:12800)
at HTMLDivElement.<anonymous> (angular-touch.js:471)
at angular.js:3097
at r (angular.js:325)angular.js:10072 (anonymous function)angular.js:7364 $getangular.js:12802 $get.k.$applyangular-touch.js:471 (anonymous function)angular.js:3097 (anonymous function)angular.js:325 rangular.js:3096 r.triggerHandlerangular.js:3111 S.(anonymous function)angular-touch.js:453 (anonymous function)angular.js:2853 (anonymous function)angular.js:325 rangular.js:2852 c

so it must be the

$.each(fooMessages, function (i, v) {
    if (v.id == message.id) {
    } 
});

part that is quite horrible.

Can you, guys, spot the error for me, please?

I am building a basic app (with MEAN web frameworks and node webkit) in order to understand angularjs better.

Here is the content of my notificationFactory.js

function notificationFactory() {

    var fooMessages = [

        {
            id: 4,
            dismissable: true,
            name: "fooooBaaar",
            function: '',
            showInTopBar: false,
            priority: "high",
            icon: 'fooIconBarBarBar',
            topBarIcon: 'fooIconIconIcon'
        },

        {
            id: 3,
            dismissable: true,
            name: "foofooBarBar",
            function: '',
            showInTopBar: false,
            priority: "high",
            icon: 'fooIconfooIcon',
            topBarIcon: 'IconIconIcon'
        },

        {
            id: 2,
            dismissable: true,
            name: "foo foo",
            function: '',
            showInTopBar: false,
            priority: "high",
            icon: 'fooBaaaaaar',
            topBarIcon: 'IconFooIcon'
        },

        {
            id: 1,
            dismissable: true,
            name: "foo",
            function: '',
            showInTopBar: false,
            priority: "high",
            icon: 'fooIcon',
            topBarIcon: 'fooIconIcon'
        },
    ]

    fooMessages.TopBarDismiss = function (message) {
        $.each(fooMessages, function (i, v) {
            if(v.id = message.id) {
                fooMessages.splice(i,1);
            }
        });

    }

    return fooMessages;

}

angular.module('fooDemo').factory('notificationFactory', notificationFactory);

I call the TopBarDismiss() function in an HTML template using:

<div class="fooDismiss" ng-click="notificationFactory.TopBarDismiss(message)">Dismiss</div>

When I check out the console after pressing my Dismiss "button", I get this:

 TypeError: Cannot read property 'id' of undefined
at notificationFactory.js:94
at Function.m.extend.each (jquery.min.js:2)
at Array.fooMessages.TopBarDismiss (notificationFactory.js:93)
at fb.functionCall (angular.js:10847)
at angular-touch.js:472
at k.$get.k.$eval (angular.js:12702)
at k.$get.k.$apply (angular.js:12800)
at HTMLDivElement.<anonymous> (angular-touch.js:471)
at angular.js:3097
at r (angular.js:325)angular.js:10072 (anonymous function)angular.js:7364 $getangular.js:12802 $get.k.$applyangular-touch.js:471 (anonymous function)angular.js:3097 (anonymous function)angular.js:325 rangular.js:3096 r.triggerHandlerangular.js:3111 S.(anonymous function)angular-touch.js:453 (anonymous function)angular.js:2853 (anonymous function)angular.js:325 rangular.js:2852 c

so it must be the

$.each(fooMessages, function (i, v) {
    if (v.id == message.id) {
    } 
});

part that is quite horrible.

Can you, guys, spot the error for me, please?

Share Improve this question edited Feb 27, 2015 at 10:52 Oleksandr T. 77.5k17 gold badges176 silver badges145 bronze badges asked Feb 27, 2015 at 10:49 George NetuGeorge Netu 2,8326 gold badges30 silver badges51 bronze badges 10
  • 5 Offtopic: I don't think you should use $.each in your Factories/Controllers. Try to limit jQuery code to directives only, and use the supplied jQuery lib given to you by AngularJS. Ontopic: Can you supply us with a fiddle? – Billy Commented Feb 27, 2015 at 10:55
  • I tried it, it behaves the same as before, because every "message" had an "id" – George Netu Commented Feb 27, 2015 at 10:55
  • 1 why not fooMessages.forEach(function(mes) { ... }); – Omri Aharon Commented Feb 27, 2015 at 10:56
  • 1 Thanks, @Billy for the good practice advice. – George Netu Commented Feb 27, 2015 at 10:56
  • 1 add a debugger; statement at the beginning of the TopBarDismiss-function and check the value of the message-variable. I suspect ng-click="notificationFactory.TopBarDismiss(message)" doesn't pass you the correct object. – Johannes Reuter Commented Feb 27, 2015 at 10:57
 |  Show 5 more comments

2 Answers 2

Reset to default 8

First, as said in the comments, you need to make sure the message object passed is really the one you're looking for.

Then, if you just want to splice you can do:

fooMessages.TopBarDismiss = function (message) {
        var index;
        for (var i = 0; i < fooMessages.length; i++) {
            if(fooMessages[i].id == message.id) {
                index = i;
                break;
            }
        }
        if (index) {
              fooMessages.splice(index,1);
        } 

}

I think you need to pass message to the inner function. In that scope, message is undefined.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论