【发布时间】:2017-11-14 12:43:35
【问题描述】:
基本上我需要等待使用Bookshelf.js 进行的insert 查询的结果,因为我需要查询提供的id 在我的数据库中插入一行
我不了解 Node 及其组件的异步行为的某些方面
所以问题出在这部分代码上:
插入方法书架
var new_timer = new Timer({
titolo: title,
time: timerVal,
created: creation,
ended: ended,
id_owner: owner
});
new_timer.save(null, {method: "insert"}).then(function(model){
if(!model)
return res.send({status: 400, url: "/add"});
associateTag(model.id_timer, tags);
return res.send({status: 200, url: "/"});
});
使用的功能
var insertAssociation = function(timerID, tags) {
return knex.table('timer_tag').insert({id_tmr: timerID, id_tg: tags.id_tag});
}
var associateTag = function(timerID, tags) {
var id_tag;
for(var i = 0; i < tags.length; i++){
getTagByName(tags[i]).then(function(result) {
console.log(result);
insertAssociation(timerID, result[0]).then(function(k) {
console.log(k);
});
});
}
}
var getTagByName = function(name) {
return knex.table('tags').select('id_tag').where('nome_tag', name);
}
【问题讨论】:
标签: javascript mysql node.js bookshelf.js