JSHint: Don't make functions within a loop. (W083)

I get the above error from JSHint for this piece of code:

try {
            Collection.insert(modifierObject, function (err, res) {
                collectionId = res._id;
            });
        } catch (err) {
            console.log('bla bla error message');
        }

I need the new _id of the insert as I will refer to it in a later step when I insert it as a reference into another collection.

How can I avoid this error message and what is wrong with my little statement?

The error is pointing to the closing bracket ) of this line: });

Thanks in advance for your help and comments!

You’re probably running this on the server, so just use the following:

collectionId = Collection.insert(modifierObject);

Oh and I don’t think that the try/catch block will catch the err from the callback anyway, as it doesn’t throw an error, it passes the error back to the callback.

1 Like

When you provide a callback, then the try/catch is useless.
Instead, you should check the first argument of the callback function.
http://docs.meteor.com/#/full/insert

1 Like

Yes, you are correct. I didn’t had the line in question in until today when I needed that res._id for later purpose, hence the standard try … error construct.

But I need to catch the error and at the same time get the res._id in case everything goes fine. So I tried to modify the code to:

Collections.insert(modifierObject, function (err, res) {
            if (err) {
                console.log('bla bla error message');
                return cb(101);    // specific error code
            } else {
                collectionId = res._id;
            }
        });

But JSHint is still complaining about the same at the last line (don’t make functions within a loop. I honestly don’t see a loop here, not sure what JSHint means by a loop.

Secondly, with the above modified code I now have the problem that the return cb(101) takes the specific error code out of this anonymous function but it doesn’t take it out of the main function which I’m running. Thus I can’t react on the specific error code from where I called the outside function.

Just to be more precise, the whole piece is like this:

blablaFunctionName = function (param1, param2, param3, cb) {
    'use strict';
    // some 280 lines of code
    Collections.insert(modifierObject, function (err, res) {
        if (err) {
            console.log('bla bla error message');
            return cb(101);    // specific error code
        } else {
            collectionId = res._id;
        }
    });
    // some 130 lines of code more
    return cb();    // indicates not a single error in the function, all went well
};

I have two other examples of code where I do get this error message from JSHint. So apart from my obvious not-making-much-sense part of the code, we still haven’t discovered or explained why JSHint gives the error message.

Hope someone can enlighten me as even after reading the official explanation from JSHint here: https://jslinterrors.com/dont-make-functions-within-a-loop I still don’t see a loop in my code nor what is wrong with it.

Does this work:

blablaFunctionName = function (param1, param2, param3, cb) {

  // your 280 lines of code here...

 try {
    let collectionId = Collections.insert(modifierObject);
  }
  catch (err) {
    return cb(101);
  }

  // your 130 lines of code here...

}

The reason for this is:

  1. Collection.insert will block while inserting and will return the collectionId correctly
  2. Collection.insert will throw an error if there is one, rather than using a callback

On the server, if you don’t provide a callback, then insert blocks until the database acknowledges the write, or throws an exception if something went wrong. If you do provide a callback, insert still returns the ID immediately. Once the insert completes (or fails), the callback is called with error and result arguments. In an error case, result is undefined. If the insert is successful, error is undefined and result is the new document ID. Ref


I am still not sure what is going on with JSHint calling out that something is wrong though?
However try this to see if it fixes the function flow…

This is finally working and non-blocking (as it runs on the server):

blablaFunctionName = function (param1, param2, modifierObject) {
    'use strict';
    var ret;
    ret = Collections.insert(modifierObject, function (err) {
        if (err) {
            console.log('Error - Insert into Collections failed');
            return '';
        }
    });
    return ret;
};

It allows me to give back the newly created _id of the Insert and in case of an error give back another string value. This way I also could get rid of that JSHint error message.

Thanks to everyone for their comments, help and suggestions, appreciated!