JavaScript Using async library to assemble all tags from database using lib async

I am using sails.js to create a tag system that has many-to-many relations with an entity called Post. The way I am doing the association is to use a database table to correlate each tagId with the postId. Each Post can have multiple tags.

After I retrieve an array of {tagId : 'tagId, postId: 'postId'} objects, I need to retrieve the tag names for each tagId. I am using the async library, but the array of tag names (String) returns an empty one, from the logging info, I can see that the return happens before the completion of the iterator function. My code is following:

var tagIdPostIds = [
{tagId : 'abcx', postId: 1},
{tagId : 'abce', postId: 1},
{tagId : 'abcd', postId: 1},
];

if (tagIdPostIds) {
        var tagsArr = []; //container of tag names

        var findTagById = function(tagIdPostIdObj, cb) {
          var tagId = tagIdPostIdObj.tagId;

          Tag.findOneById(tagId, function (err, foundTag) {
            if (err) return sendErrorMsgCode(res, "error in retrieving a tag", 401);

            if (foundTag) {
              sails.log.info('pushing in tag: ' + foundTag.tagName);
              tagsArr.push(foundTag.tagName);
            }
          });

          cb(); //without this, the program just hangs, but why??

        }; //findTagById


        var retTagNames = function(err) {
          if (err) return sendErrorMsgCode(res, "error in assembling tag names", 401);
          sails.log.info('returning tagsArr: ' + JSON.stringify(tagsArr));
          return res.json(tagsArr);
        }; //retTagNames

        async.eachSeries(tagIdPostIds, findTagById, retTagNames);
      }

After reading the documentation of async, I think async.map() may be what I need to assemble all tags for a given postId. Or my way of using async.eachSeries() is wrong?

Thanks for your help

Answer:1

Question Is it possible to know if the function was fired by a user event or an async event (like callback) without having the original event parameter? Background I'm trying to determine the event ...

Question Is it possible to know if the function was fired by a user event or an async event (like callback) without having the original event parameter? Background I'm trying to determine the event ...

  1. javascript know event listeners

I am an Admin of a Google Spreadsheet. Several times a week me and other admins have to add new users to the spreadsheet so I thought it would be easier if we could use a custom HTML dialog to do so. ...

I am an Admin of a Google Spreadsheet. Several times a week me and other admins have to add new users to the spreadsheet so I thought it would be easier if we could use a custom HTML dialog to do so. ...

How can I determine if a string contains one of the values from an array? For example: var a = ["abc","def","ghi"]; var s = "jskljfdkljflkjk abc jskfdjklsj"; for(var i=0;i<a.length;i++){ if(...

How can I determine if a string contains one of the values from an array? For example: var a = ["abc","def","ghi"]; var s = "jskljfdkljflkjk abc jskfdjklsj"; for(var i=0;i<a.length;i++){ if(...

  1. array values exist php
  2. array_values exist
  3. javascript array values exists

I'm write this code for show get point from asp button and show the point on the map: <script src="http://maps.googleapis.com/maps/api/js"></script> <script> var myCenter; ...

I'm write this code for show get point from asp button and show the point on the map: <script src="http://maps.googleapis.com/maps/api/js"></script> <script> var myCenter; ...

  1. show halfway point google maps
  2. show point on google map
  3. show north point on google maps
  4. show multiple point on google maps
  5. javascript show point on google map
  6. google show point