ReferenceError: Cannot access 'Player' before initialization
I went to the Node.JS forums and asked what could be the issue. Not a babel issue at all, just circular dependencies. For example:
// A.js
import B from './B.js'
export default class A{}
// B.js
import A from './A.js'
export default class B extends A{}
Sorry there wasn't nearly enough information to be able to figure this one out. I got a lot of help on the node.js github and someone looked through my project on github and ended up finding an instance where two modules pointed at each other.
ReferenceError: Cannot access 'players' before initialization
You are trying to modify players variable before it is being initialized , you can useMemo(which runs only if players value change) and modify sortPlayers after it is available and also you were trying to initalize players again that was causing issue
const sortedPlayers =useMemo(()=>{
return players.sort(
(a, b) => a.scorerNumber - b.scorerNumber
);
},[players])
Full Code:
import NewPointsScored from './components/NewPointsScored';
import ScoredPointsList from './components/ScoredPointsList';
import {useMemo} from 'react'
function App() {
const [scorerNumber, setScorerNumber] = useState('');
const [totPoints, setTotPoints] = useState(0);
const [players, setPlayers] = useState([]);
const sortedPlayers =useMemo(()=>{
return players?.sort(
(a, b) => a.scorerNumber - b.scorerNumber
);
},[players])
const onePointHandler = () => {
const _players = [...players];
if (scorerNumber.trim() === 0) {
return;
}
const posit = _players
.map((player) => player.scorerNumber)
.indexOf(+scorerNumber);
if (posit !== -1) {
setPlayers((players) =>
players.map(
(player, i) =>
(i = posit
? { ...player, totPoints: player.totPoints + 1 }
: player)
)
);
} else {
const newScorer = {
id: Math.floor(Math.random() * 1000),
scorerNumber: +scorerNumber,
totPoints: totPoints + 1
};
setPlayers([..._players, newScorer]);
setTotPoints(totPoints);
}
setScorerNumber("");
};
const twoPointsHandler = (e) => {
e.preventDefault();
console.log('scored 2p');
};
const threePointsHandler = (e) => {
e.preventDefault();
console.log('3p Made!');
};
return (
<div className="App">
<NewPointsScored
setScorerNumber={setScorerNumber}
scorerNumber={scorerNumber}
onOneP={onePointHandler}
onTwoP={twoPointsHandler}
onThreeP={threePointsHandler}
/>
<ScoredPointsList sortedPlayers={sortedPlayers} />
</div>
);
}
export default App;
refer sandbox:
unhandledRejection: ReferenceError: Cannot access 'firestore' before initialization
ReferenceError: Cannot access 'variable' before initialization
Line where you declare let addPoints = parseInt(args[2]);
should go before message.reply(`Set ${args[1]}'s points to ${addPoints}.`);
Declare it before your if(!data)
if(Err) console.log(err);
let addPoints = parseInt(args[2]); // <--- HERE
if(!data) {
const newData = new Data({
name: user.username,
userID: user.id,
points: parseInt(args[2])
})
newData.save().catch(err => console.log(err));
message.reply(`Set ${args[1]}'s points to ${addPoints}.`); //<--- HERE
return;
}
data.points=addPoints;
ReferenceError: Cannot access 'Discord' before initialization
Your first two lines look like they should be switched around - first "import", then use.
const Discord = require("discord.js");
const client = Discord.Client();
As per the docs:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('Pong!');
}
});
client.login('token');
Related Topics
Executing Nodejs Script File in PHP Using Exec()
Detect If String Contains Any Spaces
How to Run a Program or Batch File on the Client Side
Generate Child Row Under a Parent Row in HTML Table
How to Validate This Select Option If Not Selected
How to Remove the X-Axis from a Bar Chart Produced by Google'S Visualization API
Formatting a Number to Have Commas At Every 1000 Factor
How to Match Only the Exact Word Using Indexof or Includes in JavaScript
Detecting When User Scrolls to Bottom of Div With React Js
Nodejs MySQL Get Correct Timestamp Format
React Enable Button After All Form Fields Are Not Empty
Can We Cast a Generic Object to a Custom Object Type in JavaScript
Call External JavaScript Function from React Typescript Components
Stop Just One Dropdown Toggle from Closing on Click
Expected Linebreaks to Be 'Lf' But Found 'Crlf' Linebreak-Style