0

I am working on CLI game with Node.js and I have an issue. I have to create a save game option and I am doing it by storing the data in a JSON file. However, it gets stored as undefined.

In stats.js I am creating those values by calling a random functions that I created:

//stats.js
export let playerHealthDisplay = randomHealth()

let stats = () => {
  console.log("STATS")

  let userStats = {
    health: playerHealthDisplay,
    strength: random(),
    brains: random(),
    money: 0,
    xp: 0,
  }
}

export let userStats
export default stats

In save.js I created a function to save it in a JSON file:

//save.js
import { userStats }  from "../utils/stats.js"

const save = async () => {
  console.log(userStats) // undefined
  const data = JSON.stringify(userStats)
  writeFile("userStats.json", String(data), (err) => {
    if (err) {
      throw err
    }
  })

  console.log("Thanks for playing. -- (game saved)")
}

This works half good. It does create a JSON file called userStats.json (as it is supposed to) but it doesn't store the data correctly. It's stored undefined. I did the console.log() of imported userStats to check wheter it's imported correctly and it returns undefined.

So where did I go wrong ? Thanks in advance.

TheVSDev
  • 112
  • 13
  • 1
    I want to ask you what in the world you think `export let userStats` does. – code Mar 13 '23 at 20:27
  • 1
    `export let userStats` will export a variable that you just declared. But you've nevber assssigned anything to it. It's also not at all related to the variable created in the function. Remember: [What is the scope of variables in JavaScript?](https://stackoverflow.com/q/500431) – VLAZ Mar 13 '23 at 20:28
  • `let userStats = {....` scope is to the function it is in. – epascarello Mar 13 '23 at 20:44
  • To be more explicit, the `userStats` data you are trying to access only exists in the `stats` function. Event if it has the same name as the exported variable, the redeclaration of `userStats` with `let` makes it accessible only inside the `stats` function. Either make the `stats` function return your data or remove `let` before `userStats` (don't do this) – savageGoat Mar 13 '23 at 20:54

1 Answers1

1

You didn't assign the userStats variable before you exported it. The variable assigned in the function stats is not the same as the variable exported.

I would recommend reading through this on mdn, as I think it would help you understand what your issue is.

Based on your code and your described desired functionality, I would try doing something like this:

// stats.js
export let playerHealthDisplay = randomHealth()

let stats = () => {
  console.log("STATS")

  let userStats = {
    health: playerHealthDisplay,
    strength: random(),
    brains: random(),
    money: 0,
    xp: 0,
  }
  return userStats

}

export default stats
// save.js
import stats  from "../utils/stats.js"

const save = async () => {
  let userStates = stats();
  console.log(userStats)
  const data = JSON.stringify(userStats)
  writeFile("userStats.json", String(data), (err) => {
    if (err) {
      throw err
    }
  })

  console.log("Thanks for playing. -- (game saved)")
}
boreddad420
  • 187
  • 1
  • 7