简体   繁体   中英

How to use startsWith in node.js?

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 commands.")

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.

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
}

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