5

I am making a node app that uses the npm library MojangJS to get the uuid of a Minecraft account from the account username. However, whenever this code segment is run, I get this error: MojangJS Error: TypeError: Cannot read property 'hasOwnProperty' of undefined.

let id;
await mojangjs.getUUID(usernameParts[1]).then(uuid => {
    id = uuid;
}).catch(err => console.error(err));
SirArchibald
  • 476
  • 2
  • 7
  • 23
  • 2
    What is the value of `usernameParts[1]`? Is it undefined by any chance? – phuzi Dec 22 '20 at 16:47
  • The package you've linked hasn't been updated for 2 years, the git link is also broken. Are you sure the package is still maintained? If Mojang updated some calls, but the package did not update it, it's issue is not your code, but the package itself. – 0stone0 Dec 22 '20 at 16:57
  • @phuzi, I have `console.log`ed the value and it is not undefined. – SirArchibald Dec 22 '20 at 17:47
  • you are using both `await` and a `.then` promise chain, you should really pick 1 style and stick to it. Some more discussion here: https://stackoverflow.com/questions/54385676/whats-wrong-with-awaiting-a-promise-chain/54387912#54387912 – Andrew Lohr Dec 22 '20 at 20:04
  • this isn't much help, but maybe you don't need this package, you can try making the API calls directly yourself. Here's how that package does it - https://github.com/chriscn/mojangjs/blob/master/lib/api.js#L28 – Andrew Lohr Dec 22 '20 at 20:12

1 Answers1

1

You are using both await and .then at the same time. As Andrew you should stick to one way of doing it.

We dont know what 'usernameParts[1]' value is, but we can assume it is undefined from your comments.

I also assume you are in an async scope since you are already using the await keyword.

What i would do would be :

async function retrieveUUIDFromUsername ( username ) {
 if (!username) return;
 try {
  return await mojangjs.getUUID(username);
 } catch (err) {
  console.log(err);
  return;
 }
}

and to call this function :

let id = await retrieveUUIDFromUsername(usernameParts[1]);
if (!!id) {
 ...
}

Note that the answer can be empty so keep that in mind while using the return value of it.

Aion
  • 620
  • 1
  • 6
  • 25