0

I'm working on a save function for my game which runs on Browserify, with Electron displaying the game. I need to be able to save the game, and I thought I figured it out when I used it through the Terminal. But when I ran it in Electron, I got this error:

TypeError: Cannot read property 'toString' of undefined at http://localhost:9966/Code/Scenes/tutorial.js:410:25 at http://localhost:9966/Code/Scenes/tutorial.js:33660:20 at loop (http://localhost:9966/Code/Scenes/tutorial.js:34113:20) at http://localhost:9966/Code/Scenes/tutorial.js:34101:21 at http://localhost:9966/Code/Scenes/tutorial.js:34061:36 at dispatchError (http://localhost:9966/Code/Scenes/tutorial.js:36093:7) at http://localhost:9966/Code/Scenes/tutorial.js:35591:14 at http://localhost:9966/Code/Scenes/tutorial.js:34485:14 at IDBTransaction.getTransaction.oncomplete (http://localhost:9966/Code/Scenes/tutorial.js:31262:17)
message
:
"Cannot read property 'toString' of undefined"
stack
:
"TypeError: Cannot read property 'toString' of undefined↵    at http://localhost:9966/Code/Scenes/tutorial.js:410:25↵    at http://localhost:9966/Code/Scenes/tutorial.js:33660:20↵    at loop (http://localhost:9966/Code/Scenes/tutorial.js:34113:20)↵    at http://localhost:9966/Code/Scenes/tutorial.js:34101:21↵    at http://localhost:9966/Code/Scenes/tutorial.js:34061:36↵    at dispatchError (http://localhost:9966/Code/Scenes/tutorial.js:36093:7)↵    at http://localhost:9966/Code/Scenes/tutorial.js:35591:14↵    at http://localhost:9966/Code/Scenes/tutorial.js:34485:14↵    at IDBTransaction.getTransaction.oncomplete (http://localhost:9966/Code/Scenes/tutorial.js:31262:17)"

the code:

fs.readFile('Code/JSON files/Player.save', function(err, data) {
    var saveData = data.toString()
    saveArray = saveData.split(',')
    console.log(saveArray[0])
    console.log(saveArray[1])
    console.log(saveArray[2])

  });

EDIT: Apparently theres a wrapper for Broswerify for fs, but now fs.existsSync doesn't work.

  var load = require('../Functions/Load')
 // var save = require('../Functions/Save')
  var saveArray = []
  var saveData = load("Code/JSON files/Player.save")
console.log(saveData + "\n")
saveArray = saveData.split(',')
    console.log(saveArray[0])
    console.log(saveArray[1])
    console.log(saveArray[2])

load function:

module.exports = load
const fs = require('browserify-fs')

function load(path) {
    try {
        if (fs.existsSync(path)) {
          //file exists, read it!
          var contents = fs.readFileSync(path.toString(), 'utf8');
          return contents;
        }
      } catch(err) {
        console.error(err)
      }
}
  • data is undefined means err != null, you should log err and post it – cgcgbcbc Feb 22 '19 at 09:12
  • so apparently theres this browserify wrapper for fs that i found AFTER i posted this xD editing now with new issue – Gingka Akiyama Feb 22 '19 at 15:45
  • What is the situation of existsSync does not work? The path exists but it returns false? – cgcgbcbc Feb 24 '19 at 06:32
  • @cgcgbcbc it returns a error. if i remove `existsSync` it works, kinda, cause now `readFile` outputs `Uncaught Error: ENOENT, no such file or directory '/Code' at Object.exports.(:9966/anonymous function) [as ENOENT] (http://localhost:9966/Code/Scenes/tutorial.js:33446:13) at paths.js:36 at dispatchError (util.js:131) at levelup.js:197 at index.js:44 at IDBTransaction.getTransaction.oncomplete (idbstore.js:544)` So, now `fs` cant find the directory its right above. – Gingka Akiyama Feb 25 '19 at 15:32
  • Have you tried an absolute path for the save file? – cgcgbcbc Feb 26 '19 at 01:04
  • late response sorry and yes, still didnt work. – Gingka Akiyama Mar 01 '19 at 23:01
  • Then how about using native fs module instead of browserify-fs, like in this post https://stackoverflow.com/a/38164746/2581091 – cgcgbcbc Mar 03 '19 at 06:46
  • i had tried that, thats why i decided to use browserify-fs – Gingka Akiyama Mar 03 '19 at 07:50

0 Answers0