0

My problem is that i've got 1 array with multiple objects. And i need to do stuff for all those single objects in that array. Async

But it doesn't work?

Code:

console.log('Data: ' + data);

async.eachSeries(data, function (item, callback3) {
	console.log(item.id);
	callback3('0');
}, function done(err) {
	console.log('done');
});

And this is what the server loggs:

[root@bravo servers]# node app.js
[ { id: 1,
    ownerid: 11,
    topicid: 1,
    content: 'First comment',
    date: Sun Jun 21 2015 23:18:05 GMT+0200 (CEST) },
  { id: 2,
    ownerid: 11,
    topicid: 1,
    content: 'Another test',
    date: Mon Jun 22 2015 00:18:34 GMT+0200 (CEST) } ]
1
done

As you can see it's not doing the second one its right going to the done function?

1
  • Why are you using async.js when you don't have anything asynchronous to do? Use a plain loop. Commented Jul 6, 2015 at 13:05

1 Answer 1

3

The first argument to your inner callback is non-null (i.e. '0') which is interpreted as an error, leading the function to terminate after the first element is processed. Just make that callback3(null, '0').

For almost all callbacks in Node, the first parameter is usually an error object. (I can't think of an exception at the moment, but let's stick to "almost" anyway.)

Hope that solves your problem.

Additionally, if you're using async just for a few functions, you should probably avoid it (e.g. by doing something like this). I've had trouble with large arrays while using async. This doesn't relate to your original question; just a suggestion.

Sign up to request clarification or add additional context in comments.

2 Comments

Glad it did. If you're inclined to do that, use 0, not '0'.
i usally do but on this time when i put 0 in it it gave undefined as an error which he send back and shows on the website. my script only knows when the response obj is 0 that he doesn't have to show anything so i tried '0' but yeah its fixed now thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.