0

I'm setting up a local script to setup a website locally and was trying to get some data from mysql.

const util = require('util');
const exec = util.promisify(require('child_process').exec);

class Setup {
    constructor() {}

    async setLocalURLs(){
        const base_urls = await exec(`mysql -h${this.configFile.database.host} -u${this.configFile.database.username} -D ${this.databaseName} -p${this.configFile.database.password} -e 'SELECT id, value FROM table'`);
        console.warn(base_urls.stdout)
    }
}

module.exports = Setup;

It worked completely but I'm getting the results as new lines. Results from my commandline data below:

2747    valueA 
2748    valueB
2749    valueC
2750    valueD
2751    valueE
Is it possible to output this as an obj or array instead of newlines within 'util'? (ofcourse without split on tab etc, I can do that myself ;))
Sridhar
  • 11,466
  • 5
  • 39
  • 43
Jroon
  • 3
  • 2

1 Answers1

0

From documentation:

  • stdout <string> | <Buffer>

How you'd interpret this string is up to you. NodeJS couldn't miraculously know your intentions. That means that parsing this output and converting to objects and/or arrays is your job.

Styx
  • 9,863
  • 8
  • 43
  • 53
  • Found the solution for my question at: https://stackoverflow.com/questions/41758870/how-to-convert-result-table-to-json-array-in-mysql – Jroon Jan 10 '19 at 08:47