【发布时间】:2017-02-04 15:25:29
【问题描述】:
我只是在解析 json 时尝试访问数据库。这段小代码让我的生活变得地狱
function loopColumns(val, cb){
db.one('select tag_name from t_tag where tag_id = $1', val)
.then(function (data) {
//console.log(data.tag_name)
cb(null, data.tag_name)
});
}
var value = {"旅行活动":[1,2], "旅行方式":[3,4]};
for( i in value){
//console.log(value[i], i)
var output = '';
var outcome = '';
var str = value[i];
for(j in str){
loopColumns(str[j], function(err,res){
if(outcome=='') outcome= res;
else outcome= outcome+' , '+res;
console.log(outcome);
})
}
var output = i+' : '+outcome+' \n';
console.log('output \n'+output);
};
这是输出
输出
旅游活动:
输出
旅游方式:
好食物
美味的食物,希金
美食 , 希克 , xyz
美食 , Hicking , xyz , 测试
我喜欢输出为
旅行方式:美食,Hicking
旅行活动:xyz,测试
请救救我的命
【问题讨论】:
-
使用
Promise.. -
删除
var output的重复声明 -
我已经在使用 promise,这是异步行为的问题
-
你应该提供完整的例子......不完整的代码无济于事......
标签: javascript json node.js asynchronous callback