简体   繁体   English

在 nodejs 中将货币符号写入文件的问题

[英]Issues writing currency symbols to file in nodejs

I am trying to write to a file:我正在尝试写入文件:

private async writeToFile(data: any) {
    try {
        fs.writeFile(filePath as string, JSON.stringify(data), 'utf8', (error: any) => {
            if (error) {
                logger.error(`[JSON] Error while saving file : ${error}`);
            }
            logger.info('The file has been saved!');
        });
    } catch (error) {
        logger.error(`[JSON] Error while saving file : ${error}`);
    }
}

where data has:其中数据有:

var data = [{label:'Egyptian Pound £', value: 'E£'}, {"label":"Albanian Lek-AL","value":"AL"}];

When I write to file, the characters are saved as {label: Egyptian Pound E , value: E }当我写入文件时,字符被保存为{label: Egyptian Pound E , value: E }

The data array is created from a multi line string returned from server:数据数组是从服务器返回的多行字符串创建的:

Egyptian Pound|E£   
Albanian Lek|AL    

Code to create the data array:创建数据数组的代码:

const currencyArr = response
    .split('\n')
    .map(val => val.trim())
    .reduce((arr, currencyString) => {
        arr.push({
            label: currencyString.split('|')[0] + '-' + currencyString.split('|')[1],
            value: currencyString.split('|')[1]
        });
        return arr;
    }, []);
this.writeToFile(currencyArr);

I am not sure why this is happening.我不确定为什么会这样。 As per docs, node supports UTF-8 encoding by default根据文档,节点默认支持 UTF-8 编码

The only reason I can find this kind of thing happen is if your JS file is the one not encoded in UTF8.我能发现这种事情发生的唯一原因是,如果您的 JS 文件不是用 UTF8 编码的文件。

Make sure the JS file is saved in the UTF8 encoding, so the string in your script can be saved to the corresponding encoding.确保JS文件保存为UTF8编码,这样你脚本中的字符串就可以保存为对应的编码。

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

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