Im trying to make football player generator. Im using mongodb estimatedDocumentCount() to count amount of documents with certain data to increase ID of eash footballer by 1.
const footballersCounter = async () => {
const counter = await dbo.collection('footballers').estimatedDocumentCount({});
console.log(counter);
return counter;
}
Since its async function i've to make fetching data async too:
const fetchFootballerBio = async (nameType) => {
let output = [];
switch (nameType) {
case 'names':
output = firstnames_m;
break;
case 'surnames':
output = surnames;
break;
case 'footballerId':
output = await (footballersCounter() 1);
break;
}
return { data: output };
}
For names and surnames (it is json file with huge amount of names) I use randomising function:
function randomiser(warray) {
var randomNumber = Math.floor(Math.random() * warray.length);
return warray[randomNumber];
}
When function fetchFootballerBio is not async = names and surnames are working, but i footballerId gives [Promise pending]. When it is async i receive: TypeError: Cannot read properties of undefined (reading 'length')
Randomiser calls inside the function to generate the player:
function generateFootballer(teamName, nationality) {
const firstNames = fetchFootballerBio('names');
const lastNames = fetchFootballerBio('surnames');
const footballerIds = fetchFootballerBio('footballerId');
const firstName = randomiser(firstNames.data);
const lastName = randomiser(lastNames.data);
const footballerId = footballerIds.data;
dbo.collection('footballers').insertOne({
firstName: firstName,
lastName: lastName,
age: age,
nation: nationalitySetup,
footballerId: footballerId,
position: position,
teamName: teamNameSetup,
skills: [{ speed: footballerSpeed }, { shooting: footballerShooting }, { control: footballerControl }, { passing: footballerPassing }, { defending: footballerDefending }, { handling: footballerHandling }]
})
Any suggestions what can i do?
I've tried to run randomiser outside of switch, it worked perfectly for names and surnames.
CodePudding user response:
I can spot two things:
output = await (footballersCounter() 1);
should beoutput = (await footballersCounter()) 1;
, and- You need to make
generateFootballer
async
andawait
the results. You are currently looking for a property named "data" on a Promise, which does not exist and so will supplyundefined
torandomiser
(and this blows-up when you ask for thelength
property ofundefined
).
async function generateFootballer(teamName, nationality) {
const firstNames = await fetchFootballerBio('names');
const lastNames = await fetchFootballerBio('surnames');
const footballerIds = await fetchFootballerBio('footballerId');
const firstName = randomiser(firstNames.data);
const lastName = randomiser(lastNames.data);
const footballerId = footballerIds.data;
dbo.collection('footballers').insertOne({
firstName: firstName,
lastName: lastName,
age: age,
nation: nationalitySetup,
footballerId: footballerId,
position: position,
teamName: teamNameSetup,
skills: [{ speed: footballerSpeed }, { shooting: footballerShooting }, { control: footballerControl }, { passing: footballerPassing }, { defending: footballerDefending }, { handling: footballerHandling }]
})
}