简体   繁体   English

控制台日志显示消息但文件中没有任何内容

[英]Console log shows message but nothing in file

I'm trying to save output to a file.我正在尝试将 output 保存到文件中。 I can see the error message from server in console.log (see bellow) but for some reason it's not written in file.我可以在 console.log 中看到来自服务器的错误消息(见下文)但由于某种原因它没有写在文件中。 The file is empty.该文件为空。 Any ideas?有任何想法吗?

const fileName = `response ${data.item.name}.json`; // create file from response of each request 
const content = data.response.stream.toString(); // content of response   
console.log("JSON response: ", content);


fs.writeFile(fileName, content, function(error) { // The file itself
  if (error) {
    console.error(error);
  }
});

What I see in console log: I need that "JSON response" be in file我在控制台日志中看到的内容:我需要将“JSON 响应”放入文件中

→ Create pce_test
  POST http://<<server>>:<<port>>/apiv2/servers  JSON response:  Error creating Integration Server: server name in use
[409 Conflict, 886B, 82ms]

视觉工作室输出

The content has to be in the JSON format, if you want to write a JSON file.如果要编写 JSON 文件,内容必须采用 JSON 格式。

const content = `{"key": "${data.response.stream.toString()}"}`; // content of response

If you want to write the object properties in a new line with a tab, you can use something like below;如果你想用一个标签在一个新的行中写 object 属性,你可以使用类似下面的东西;

const content = JSON.stringify(`{\n \t "key": "${data.response.stream.toString()}"\n}`); 
fs.writeFile(fileName, JSON.parse(content), (error) => { // The file itself
  if (error) {
    console.error(error);
  }
});

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

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