LoginSignup
1
2

More than 3 years have passed since last update.

【N予備校】例外処理 JSONファイルが読み込めない!原因を探る方法

Last updated at Posted at 2018-07-28

まずはエラーの部分を探る

try{

const data = fs.readFileSync(fileName, 'utf8');

tasks = new Map(JSON.parse(data));

}  catch (ignore){

console.log(ignore); // この行を追加

console.log(fileName + 'から復元できませんでした');

}

console.log(ignore); を追加することにより catch (ignore) した内容を出力できる。

vagrant@ubuntu-xenial:~/workspace/todo$ node

> 'use strict';

'use strict'

> const todo = require(`./`);

ReferenceError: fileNaame is not defined

at Object.<anonymous> (/home/vagrant/workspace/todo/index.js:8:32)

at Module._compile (module.js:643:30)

at Object.Module._extensions..js (module.js:654:10)

at Module.load (module.js:556:32)

at tryModuleLoad (module.js:499:12)

at Function.Module._load (module.js:491:3)

at Module.require (module.js:587:17)

at require (internal/module.js:11:18)

at repl:1:14

at ContextifyScript.Script.runInThisContext (vm.js:50:33)

./tasks.jsonから復元できませんでした

undefined

これで原因が
ReferenceError: fileNaame is not defined at Object. (/home/vagrant/workspace/todo/index.js:8:32)
だった事が分かりました。

あとは修正する

該当部分を修正し

    vagrant@ubuntu-xenial:~/workspace/todo$ node

    > 'use strict';

    'use strict'

    > const todo = require(`./`);

    undefined

    > todo.list();

    [ 'ノートを買う' ]

これでJSONファイルが読み込めました!

N予備校って優しい解説から急に難しくなりますよね

独自フォーラムとかありますけどQiita的な記事投稿ができないのが難点。

※2020年1月更新

MENTAでコードレビューやプログラミング勉強相談を行っています!

お気軽にメッセージを下さい!
プランはこちらになります!

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2