admin管理员组文章数量:1391976
I'm trying to populate an array using a MYSQL query on a table that takes all rows and pushes the rows to WordList.
I can print each line within the method fine, but when I go out of the scope of that method it doesn't push anything to Wordlist.
function getParrotMessage() {
wordList = [];
console.log(wordList);
// Implementation
getWord('result', function (err, result) {
console.log(result); // works, prints the row data in MySQL table
wordList.push(result); // doesn't work
});
console.log(wordList);
return parrot_message;
}
// Method
function getWord(word, callback) {
var query = con.query('SELECT * FROM word_table');
query.on('result', function (row) {
callback(null, row.word);
});
};
wordlist: []
wordlist shows up as an empty array.
Any help would be greatly appreciated, just beginning with javascript and node.js
I'm trying to populate an array using a MYSQL query on a table that takes all rows and pushes the rows to WordList.
I can print each line within the method fine, but when I go out of the scope of that method it doesn't push anything to Wordlist.
function getParrotMessage() {
wordList = [];
console.log(wordList);
// Implementation
getWord('result', function (err, result) {
console.log(result); // works, prints the row data in MySQL table
wordList.push(result); // doesn't work
});
console.log(wordList);
return parrot_message;
}
// Method
function getWord(word, callback) {
var query = con.query('SELECT * FROM word_table');
query.on('result', function (row) {
callback(null, row.word);
});
};
wordlist: []
wordlist shows up as an empty array.
Any help would be greatly appreciated, just beginning with javascript and node.js
Share Improve this question edited Nov 16, 2016 at 18:33 user1920076 asked Nov 16, 2016 at 18:27 user1920076user1920076 1031 gold badge2 silver badges14 bronze badges 2-
Your method
getWord
is asynchronous ! So the secondconsole.log(wordList);
is printed before any results are returned (before you even callwordList.push(result);
for the first time) – Molda Commented Nov 16, 2016 at 19:10 - So how can I actually retrieve that result data so I can manipulate it and do various other thing. I don't need to print it. – user1920076 Commented Nov 16, 2016 at 20:53
1 Answer
Reset to default 3Your method getWord is asynchronous!
So the second console.log(wordList);
is printed before any results are returned (before you even call wordList.push(result);
for the first time)
Also since you query db(which is asynchronous) in getParrotMessage function you need to use callback (or Promise or whatever else there is that can be used) instead of return statement.
function getParrotMessage(callback) {
getWord('result', function (err, result) {
if(err || !result.length) return callback('error or no results');
// since result is array of objects [{word: 'someword'},{word: 'someword2'}] let's remap it
result = result.map(obj => obj.word);
// result should now look like ['someword','someword2']
// return it
callback(null, result);
});
}
function getWord(word, callback) {
con.query('SELECT * FROM word_table', function(err, rows) {
if(err) return callback(err);
callback(null, rows);
});
};
now use it like this
getParrotMessage(function(err, words){
// words => ['someword','someword2']
});
本文标签: javascriptHow do I return callback of MySQL query and push to an array in NodejsStack Overflow
版权声明:本文标题:javascript - How do I return callback of MySQL query and push to an array in Node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744753589a2623326.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论