Confused as to why this Array (?) object is not behaving like an array. I am making an API call to a cloud function, which returns an object containing an array of games. I setState this array of games, but the child component does not recognize it as an array.
"games" does not have a length property.
Cloud function:
exports.getUserOwnedGames = functions.https.onCall(id => {
return new Promise((resolve, reject) => {
return steam.getUserOwnedGames(id).then(res => {
resolve(res);
});
});
});
Client call:
export default class API {
static getUserOwnedGames(id) {
return functions
.httpsCallable("getUserOwnedGames")("1234567890") // TEMP
.then(result => {
const userOwnedGames = result.data;
return { games: userOwnedGames };
});
}
}
Parent component:
class App extends React.Component {
constructor() {
super();
this.state = {
games: false
};
}
loadData = () => {
API.getUserOwnedGames("test").then(result => {
console.log(result);
console.log(result.games);
this.setState({ games: result.games });
});
};
componentDidMount() {
this.loadData();
}
render() {
const { games } = this.state;
return (
<div className="App">
<header className="App-header">
<GameLib games={games} />
</header>
</div>
);
}
}
Child component:
function GameLib(games) {
if (games.length > 0) {
return games.map(game => {
return (
<div>
<p>{game.name}</p>
</div>
);
});
} else {
return (
<div>
<p>No Games! Boring</p>
</div>
);
}
}
console.log(result):
{games: Array(32)}
Array.from(games).