最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use startsWith in node.js? - Stack Overflow

programmeradmin4浏览0评论
let botmod = staff.get(`${message.author.id}`)
  if (botmod.startsWith !== "Bot")
    return message.channel.send("Unfortunately, you are a server mod. You can't use bot mands.")

The above is an example of me trying to use .startsWith(). However, I don't think I am using it correctly.
It triggers that message, even if the message does have Bot in it.
There are no errors logged in the console.

let botmod = staff.get(`${message.author.id}`)
  if (botmod.startsWith !== "Bot")
    return message.channel.send("Unfortunately, you are a server mod. You can't use bot mands.")

The above is an example of me trying to use .startsWith(). However, I don't think I am using it correctly.
It triggers that message, even if the message does have Bot in it.
There are no errors logged in the console.

Share Improve this question edited Sep 9, 2020 at 8:01 Mineko Kayui 7614 silver badges15 bronze badges asked Sep 8, 2020 at 22:42 Magical CatMagical Cat 781 silver badge8 bronze badges 2
  • There's not enough in your post to provide an answer since there's no way to tell what staff.get returns; however, String.prototype.startsWith is a method and needs parentheses. – benbotto Commented Sep 8, 2020 at 22:45
  • Documentation is always your friend developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – sinanspd Commented Sep 8, 2020 at 22:46
Add a ment  | 

3 Answers 3

Reset to default 3

startsWith is a function, you pass the value you want to check and it will return a boolean, so

if (botmod.startsWith("Bot")) 
  ...
} 

It is a function:
If you want the string to start with Bot,

if(botmod.startsWith("Bot")){
  // ...do stuff
}

or If you don't want the string to start with Bot,

if(!botmod.startsWith("Bot")){
  // ...do stuff
}

However, you can only run .startsWith() on Strings.

startsWith returns a boolean. So you want

const foo = 'bar';

if(foo.startsWith('thing')) {
   // do stuff
}
发布评论

评论列表(0)

  1. 暂无评论