简体   繁体   English

Node.js util.promisify-将stdout输出为obj或数组而不是换行

[英]Node.js util.promisify - Output stdout as obj or array instead of new lines

I'm setting up a local script to setup a website locally and was trying to get some data from mysql. 我正在设置本地脚本以在本地设置网站,并试图从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'? 是否可以将其输出为obj或数组,而不是'util'中的换行符? (ofcourse without split on tab etc, I can do that myself ;)) (当然,在选项卡等上没有拆分,我可以自己做;))

From documentation : 文档

  • stdout <string> | stdout <string> | <Buffer>

How you'd interpret this string is up to you. 您如何解释此字符串取决于您。 NodeJS couldn't miraculously know your intentions. NodeJS无法奇迹般地了解您的意图。 That means that parsing this output and converting to objects and/or arrays is your job. 这意味着解析此输出并将其转换为对象和/或数组是您的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM