2017-08-05 49 views
0

的Javascript sqlite3的字符串。當我查詢我的SQLite數據庫中db.each字符串不會追加使用加法運算符不附加

var messages = "Global Message History: \n\n"; 

db.each(sql, [Article], (err, row) => { 
    if (err) { 
    throw err; 
    } 

    console.log(`[${row.message_timestamp}] <@${row.msg_id}> in #${row.channel_name} on Server "${row.server_name}": ${row.message_content}`); //Outputs all messages 
    messages += `[${row.message_timestamp}] <@${row.msg_id}> in #${row.channel_name} on Server "${row.server_name}": ${row.message_content}\n`; // Doesn't work 

}); 

sendMsg(messages); 

sendMsg();只能與所有的消息一旦被執行,但它似乎沒有工作,輸出僅Global Message History: \n\n

回答

0

一旦此方法是異步的,你應該叫sendMsg()complete說法db.each爲:

var messages = "Global Message History: \n\n"; 

db.each(sql, [Article], (err, row) => { 
    if (err) { 
    throw err; 
    } 

    console.log(`[${row.message_timestamp}] <@${row.msg_id}> in #${row.channel_name} on Server "${row.server_name}": ${row.message_content}`); //Outputs all messages 
    messages += `[${row.message_timestamp}] <@${row.msg_id}> in #${row.channel_name} on Server "${row.server_name}": ${row.message_content}\n`; // Doesn't work 
}, function(){ sendMsg(messages) }); 

希望這會有所幫助。

+0

真棒的作品,不知道這存在。謝謝你的協助! – SelectJB