简体   繁体   中英

Receiving error message when initialising JavaScript function

So below is my javascript code. It's wrapped in module.exports, but that's not the error.

 parseMention = (arg, asId = false) => {
    if(!arg.startsWith('<@') && arg.endsWith('>')) return undefined
    let id = arg.slice(2, -1)
    if(id.startsWith('!')) id.slice(1)
    if(asId === true) return id
    if(asId === false) return bot.users.cache.get(id)
}

despite it seeming correct to me, I get the following error message:

SyntaxError: Invalid shorthand property initializer

Any help would be much appreciated

It's wrapped in module.exports, but that's not the error.

I'm pretty sure there is the error.

This part of your code won't throw a SyntaxError: Invalid shorthand property initializer error. It would be great to see your module.exports , but it's 99.9% that you're trying to export an object like this and the = after parseMention is an invalid shorthand property initialiser:

// This is invalid syntax
module.exports = {
  parseMention = (arg, asId = false) => {
    if (!arg.startsWith('<@') && arg.endsWith('>')) return undefined;
    let id = arg.slice(2, -1);
    if (id.startsWith('!')) id.slice(1);
    if (asId === true) return id;
    if (asId === false) return bot.users.cache.get(id);
  }
}

This should work:

module.exports = {
  parseMention(arg, asId = false) {
    if (!arg.startsWith('<@') && arg.endsWith('>')) return undefined;
    let id = arg.slice(2, -1);
    // it won't do anything, slice won't mutate id and you don't return
    if (id.startsWith('!')) id.slice(1);
    if (asId === true) return id;
    if (asId === false) return bot.users.cache.get(id);
  },
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM