So I am playing around with node.js and trying to get push some data to an object from a function. Everything is being parsed correctly with the regex, I guess I just don't understand how to implement what I am doing correctly. I am getting undefined on the json objects after I parse the game data.
const diplo = `!report Game Type: Diplo
1: <@12321321421412421>
2: <@23423052352342334>
3: <@45346346345343453>
4: <@23423423423523523>`
var diplo_data = {players:[]};
async function gameType(game) {
let data = {};
let game_types = [{id: 1, name: 'Diplo'}, {id: 2, name: 'Always War'}, {id: 3, name: 'FFA'}
, {id: 4, name: 'No Diplo'}, {id: 5, name: 'Team'}, {id: 6, name: 'Duel'}, {id: 7, name: 'No War'}];
let o = {}
let reType = /!report Game Type:\s+?(\d|\w+)\s?(\w+\s?(\w+)?)?\n/gi
let match = reType.exec(game)
if(match[1]){
for (var j = 0; j < game_types.length; j++) {
if ((game_types[j].name).toLowerCase() === (match[1]).toLowerCase()) {
data.type = game_types[j].id;
break;
}
}
}
if(match[2]){
data.moddifier = match[2]
}
console.log(data)
await (data)
}
async function main() {
console.log("Reading diplo data...\n\r")
try {
diplo_data = await gameType(diplo)
console.log(diplo_data)
} catch (err) {
return 'No game type!';
}
}
main();
returnstatement beforeawait (data). But the main question is why do you useasync/awaitthere?gameTypeis not asynchronous function.