最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I return callback of MySQL query and push to an array in Node.js? - Stack Overflow

programmeradmin0浏览0评论

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 second console.log(wordList); is printed before any results are returned (before you even call wordList.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
Add a ment  | 

1 Answer 1

Reset to default 3

Your 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']

});
发布评论

评论列表(0)

  1. 暂无评论