ga-wdi-boston / game-project

Other
7 stars 102 forks source link

Trouble with finding an imported object's property #833

Closed CoreyFedde closed 7 years ago

CoreyFedde commented 7 years ago

Hi,

I'm importing an object from another file named gamesPlayed. When I console.log gameActions.gamesPlayed I get an object with a length property nested within it. But when I console.log gameActions.gamesPlayed.length, it comes back as undefined.

Any thoughts on how to access it?

gameActions file

let gamesPlayed = {}
const getStats = function (event) {
  stats()
  .then(function (data) {
    console.log('success')
    console.log(data.games.length)
    gamesPlayed.length = data.games.length
  })
  .catch(function (data) {
    console.log('Nope')
  })
}

logic file

const getNewGame = function (event) {
  console.log('button clicked')
  $('#game-text').text('New game!')
  turnCounter = 0
  // Create an array to represent the game-board
  $('.game').each(function () {
    $(this).text('')
  })
  gameActions.getStats()
  let gameCount = gameActions.gamesPlayed.length
  console.log('this is gameActions.gamesPlayed ', gameActions.gamesPlayed)
  console.log('this is gameActions.gamesPlayed.length ', gameActions.gamesPlayed.length)
  $('#gamesDisplay').text(gameCount)
}

Results of the last two console logs:

" this is gameActions.gamesPlayed
Object {} length: 82 proto: Object

" this is gameActions.gamesPlayed.length undefined"

benjimelito commented 7 years ago

One possible issue is that I believe length is a reserved property name, and may cause some weird behavior if you create your own property called length. Try calling your property something else - if you still get an issue, let me know and I'll come check it out.

CoreyFedde commented 7 years ago

Still showing up as undefined

CoreyFedde commented 7 years ago

There was an issue with an asynchronous function. By placing the line to display text as the value of the object.length in the initial function, it was fixed.