使用\a BEL (0x07)
\b BS (0x08)
\d DEL (0x7f)
\e ESC (0x1b)
\f FF (0x0c)
\n NL (0x0a)
\r CR (0x0d)
\s SP (0x20)
\t TAB (0x09)
\v VT (0x0b) \
\uhhh 1–6 hex digits
\xhh 2 hex digits
将节点写出后,以下代码无法在节点中要求JSON文件。检查文件时,结尾没有奇怪的字符,最后一个JSON对象正确地用JSON.stringify(...)
和}
关闭,以结束数组。
]
答案 0 :(得分:0)
In your code, the line that attempts to read the JSON is being executed before the file is actually written.
The callback function (where you write the confirmation message to the console), is when you know the file is written. If you were to re-write it as follows, you should see the expected result:
fs.writeFile('marketData.json', JSON.stringify(costArray), 'utf8', function(){
// Now we're sure the file has been written, so we can read it.
console.log("Market data written to file");
const marketPrices = require('./marketData');
});
// Anything here will get executed *immediately* after you *start* writing the file,
// which does not guarantee the file has actually been written yet (and in most cases,
// it won't have been).