How to set a session inside a loop?

Trying to set a session inside a file loop for upload using CollectionFS. Everything works fine except the session is not set. Why?

for (var i = 0, ln = files.length; i < ln; i++) {
    Images.insert(files[i], function (err, fileObj) {
      Session.set("imgid", fileObj._id);

Assuming this is being called client side (otherwise Session won’t exist), are you sure the upload is working properly? Maybe double check the value of err in your callback; if there are any problems CollectionFS returns void 0 (aka undefined) for the value of fileObj.

Yes client and it works, the fileObj._id is returned.

Okay - maybe just make sure it isn’t being set then overwritten. In the example you provided you’re looping over potentially multiple image inserts, but always storing the resulting fileObj._id against the same session variable. Depending on how your loop runs, you might be clobbering the session variable with a invalid value. Take the loop out of the equation to start with, testing just one insert to see what happens.

Thanks. Must be it. Will try that.