【问题标题】:Receiving error message when initialising JavaScript function初始化 JavaScript 函数时收到错误消息
【发布时间】:2021-05-15 13:21:26
【问题描述】:

所以下面是我的 javascript 代码。它包含在 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)
}

尽管对我来说似乎是正确的,但我收到以下错误消息:

SyntaxError: 无效的速记属性初始化器

任何帮助将不胜感激

【问题讨论】:

  • 请在您调用此方法的位置添加代码部分...
  • 你在这里做什么,//if(id.startsWith('!')) id.slice(1)// id.slice(1) 返回另一个你没有分配的数组将数组返回到任何位置。

标签: javascript discord discord.js


【解决方案1】:

它包含在 module.exports 中,但这不是错误。

我很确定有错误。

这部分代码不会引发SyntaxError: Invalid shorthand property initializer 错误。很高兴看到您的 module.exports,但有 99.9% 的情况是您试图导出这样的对象,而 parseMention 之后的 = 是无效的速记属性初始化程序:

// 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);
  }
}

这应该可行:

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);
  },
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 2021-06-24
    相关资源
    最近更新 更多