Reputation: 2261
I am trying to create an angular service but getting an error. This is the service:
var app = angular.module('plunker', []);
// Filter Service that returns records with crdamt positive:
app.factory('FilterService', function() {
return function(d) {
var filteredData = [];
console.log(d);
d.forEach(function(item)
{
if (item.cramt > 0) {
filteredData.push(item);
}
});
return filteredData;
}
});
In my unit test however we get:
it('should return transactions where credit is positive', function() {
var jsonData = {
"transactions": [{
"date": "1/1/2000",
"desc": "Purchase",
"cramt": 50,
"dbamt": 0
}, {
"date": "1/1/2002",
"desc": "Transaction",
"cramt": 110,
"dbamt": 10
}]
};
var filteredRecords = FilterService(jsonData);
expect(filteredRecords).toEqual({
"date": "1/1/2000",
"desc": "Purchase",
"cramt": 50,
"dbamt": 0
});
});
Why do I get an error:
TypeError: d.forEach is not a function
plunkr:http://plnkr.co/edit/jdg0wEj1zSDTbqojBAoc?p=preview
Upvotes: 1
Views: 2954
Reputation: 15292
you can define filter in this way.
app.factory('FilterService', function() {
return function(d) {
var filteredData = [];
var boolDataISFound = false;
console.log(d.transactions)
d.transactions.forEach(function(item,index)
{
if (item.cramt > 0) {
filteredData.push(item);
}
});
return filteredData[0];
}
});
and here is the expectation
it('should return transactions where credit is positive', function() {
var jsonData = [{
"transactions": [{
"date": "1/1/2000",
"desc": "Purchase",
"cramt": 50,
"dbamt": 0
}, {
"date": "1/1/2002",
"desc": "Transaction",
"cramt": 110,
"dbamt": 10
}]
}];
var filteredRecords = FilterService(jsonData[0]);
expect(filteredRecords).toEqual({
"date": "1/1/2000",
"desc": "Purchase",
"cramt": 50,
"dbamt": 0
} );
});
Here is the Plunker
Upvotes: 0
Reputation: 2671
Please look at the updated plnkr. forEach is used for the arrays only and your 'd' returns an object.
Following is the link to the plnkr :
``http://plnkr.co/edit/o65e0xEsBiW0jpawhGse?p=preview
Upvotes: 0
Reputation: 1776
forEach
method is available on arrays
.
Looking at your jsonData
you can do this:
app.factory('FilterService', function() {
return function(data) {
var filteredData = [];
console.log(data.transactions);
data.transactions.forEach(function(item)
{
if (item.cramt > 0) {
filteredData.push(item);
}
});
return filteredData;
}
});
P.S: Please read the error messages. It clearly says that d.forEach
is not a functions. It is like trying to define an object like:
var person = {
'name':'Paul'
};
and trying to access person.height()
. The height
is not a property of person
object and you are trying to access undefined property as a function
.
Upvotes: 3
Reputation: 5061
Your test is trying to filter the entire object instead of the array of transactions.
var filteredRecords = FilterService(jsonData);
expect(filteredRecords).toEqual({
"date": "1/1/2000",
"desc": "Purchase",
"cramt": 50,
"dbamt": 0
});
should be
var filteredRecords = FilterService(jsonData.transactions);
expect(filteredRecords).toEqual([{
"date": "1/1/2000",
"desc": "Purchase",
"cramt": 50,
"dbamt": 0
}]);
Upvotes: 0
Reputation: 4635
Just try. I'm expecting variable d
as an array.
angular.forEach(d, function(item){
if (item.cramt > 0) {
filteredData.push(item);
}
});
You're not using the right syntax for forEach
function of angular js. Please refer this angular.forEach directive
Upvotes: 0