簡體   English   中英

初始化 JavaScript function 時收到錯誤消息

[英]Receiving error message when initialising JavaScript function

所以下面是我的 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:速記屬性初始值設定項無效

任何幫助將非常感激

它包含在 module.exports 中,但這不是錯誤。

我很確定有錯誤。

這部分代碼不會拋出SyntaxError: Invalid shorthand property initializer錯誤。 很高興看到您的module.exports ,但是您嘗試像這樣導出 object 並且= after 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);
  },
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM