貼文討論:後端 JS 訓練一:第7課 ── 學習 node 匯入/匯出模組


留言回覆:[作業](https://github.com/superyngo/nodePractice) 幾個問題: readlinine-sync還是無法正確輸入中文,只能輸入英文 readline和readlinine-sync同時載入時螢幕不會顯示出輸入的字


const fs = require("fs");
const readlineSync = require("readline-sync");

let todos = JSON.parse(fs.readFileSync("todos.json"));
todosList = todos.map((todo, index) => `#${index} ${todo.title}`).join("\n");

const toUpdateIndex = readlineSync.question(
  `您要更新哪個待辦事項?
${todosList}\n`
);

const readline = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

readline.question("您想把內容更新成什麼?\n", function (toUpdateContent) {
  todos[toUpdateIndex].title = toUpdateContent;
  console.log("更新事項:" + toUpdateContent);
  fs.writeFileSync("todos.json", JSON.stringify(todos));
  process.exit(0);
});

1.readlineSync無法顯示中文的問題有看前輩的方式解決,不過問題是顯示的出來,輸入的中文又會變成亂碼。 2.同時載入readlineSync和readline時,readlin的載入和呼叫間不能插入readlineSync,不然readline不會停下來等answer直接結束。然後readlineSync會變成輸入時看不到輸入的文字。我是在windows11使用vs code,也許環境的關係導致?


此人尚未填寫簡介。