使用异步库汇编数据库中的所有标记
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.
我正在使用sails.js创建一个标签系统,该系统与名为Post的实体具有多对多关系。我正在进行关联的方式是使用数据库表将每个tagId与postId相关联。每个帖子可以有多个标签。
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:
在检索{tagId:'tagId,postId:'postId'}对象的数组后,我需要检索每个tagId的标记名称。我正在使用异步库,但标记名称(String)数组返回一个空的,从日志信息中,我可以看到返回发生在迭代器函数完成之前。我的代码如下:
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?
在阅读了异步文档之后,我认为async.map()可能是我需要为给定的postId汇编所有标签。或者我使用async.eachSeries()的方式是错误的?
Thanks for your help
谢谢你的帮助
1 个解决方案
#1
Just wanted to post this correct answer by incorporating @Ben's answer in his comments from above so others can easily find what worked for me:
只是想通过在上面的评论中加入@Ben的答案来发布这个正确答案,以便其他人可以轻松找到对我有用的内容:
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(); //this must be placed inside an async call to make it work!
});
}; //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);
}
更多相关文章
- 如何获取knockoutjs可观察数组的下一个元素?
- 显示json数组中的所有项目
- 图表。js数据数组使用PHP, MySQL。如何从JSON数组定义数据源?
- 将JavaScript对象转换为要插入关系数据库的数组
- javascript数组和对象是否有设置顺序?
- 从另一个数组中删除数组的内容。
- 从两个数组生成JSON
- HTML5绘图之Canvas标签 绘制坐标轴
- 如何将图像(PNG)转换为2D数组(二进制图像)?