javascript - SyntaxError: Reserved word "function" - Stack Overflow

I'm trying to code a script for Github's Hubot that uses TooTallNate's Node-Spotify-Web

I'm trying to code a script for Github's Hubot that uses TooTallNate's Node-Spotify-Web to play music through spotify, and I'm somewhat new to CoffeeScript (What Hubot scripts are written in). I wrote out the first mand "Play" here:

lame = require('lame')
Speaker = require('speaker')
Spotify = require('spotify-web')

username = "INSERTUSERNAMEHERE"
password = "INSERTPASSWORDHERE"

robot.respond /play (.*)/i, (message) ->
  uri = message.match[1]
  Spotify.login(username, password, function (err, spotify)) {
        if (err) throw err;
        console.log('Playing: %s - %s', track.artist[0].name, track.name)
}
  spotify.get(uri, function(err, track){
        if err throw err;
        message.send("Playing:" + track.artist[0].name, track.name)
        })

Upon running bin/hubot I got the Error "Syntax Error, Reserved word "function" so I said, ok and changed 'function' to '->' as remend in another StackOverflow question. Making it so it appeared as:

But still get the error

ERROR Unable to load /home/xbmc/cbot/lisa/scripts/spotify: SyntaxError: reserved word "function"

Is it because of the dependencies? I'm really stuck here.

I'm trying to code a script for Github's Hubot that uses TooTallNate's Node-Spotify-Web to play music through spotify, and I'm somewhat new to CoffeeScript (What Hubot scripts are written in). I wrote out the first mand "Play" here:

http://pastebin./Pp6mqucm

lame = require('lame')
Speaker = require('speaker')
Spotify = require('spotify-web')

username = "INSERTUSERNAMEHERE"
password = "INSERTPASSWORDHERE"

robot.respond /play (.*)/i, (message) ->
  uri = message.match[1]
  Spotify.login(username, password, function (err, spotify)) {
        if (err) throw err;
        console.log('Playing: %s - %s', track.artist[0].name, track.name)
}
  spotify.get(uri, function(err, track){
        if err throw err;
        message.send("Playing:" + track.artist[0].name, track.name)
        })

Upon running bin/hubot I got the Error "Syntax Error, Reserved word "function" so I said, ok and changed 'function' to '->' as remend in another StackOverflow question. Making it so it appeared as:

http://pastebin./dEw0VrH5

But still get the error

ERROR Unable to load /home/xbmc/cbot/lisa/scripts/spotify: SyntaxError: reserved word "function"

Is it because of the dependencies? I'm really stuck here.

Share Improve this question edited Dec 22, 2013 at 5:34 Alex Wayne 187k52 gold badges328 silver badges360 bronze badges asked Dec 22, 2013 at 5:24 ChiChuChiChu 631 silver badge4 bronze badges 6
  • 2 Are you using CoffeeScript, as your question says, or JavaScript, as you put in the tags? – Chris Hayes Commented Dec 22, 2013 at 5:26
  • 1 This doesn't look right spotify.get(uri, ->(err, track){. Also all you need to put formatted code in SO (or anywhere online) is convert tabs to spaces. – elclanrs Commented Dec 22, 2013 at 5:27
  • CoffeeScript, I apologize I pletely forgot to add a tag for it. – ChiChu Commented Dec 22, 2013 at 5:29
  • Code has to go in the question, not in an external code-hosting service. – user229044 Commented Dec 22, 2013 at 5:30
  • The code in the pastebins is incorrect; a mix of JS and Coffeescript. The question as-is qualifies as "must demonstrate a minimal understanding of the problem". – elclanrs Commented Dec 22, 2013 at 5:32
 |  Show 1 more ment

2 Answers 2

Reset to default 4

One of the very first sections of the coffee script documentation is how to declare functions. You don't just change the word function to ->. It's not that simple. In Javascript functions are function(args) { body }, but in Coffee Script it's (args) -> body

But for brevity, when you have this:

Spotify.login(username, password, function (err, spotify)) {

That's not going to work CoffeeScript, because that's not the syntax for declaring functions. You want:

Spotify.login username, password, (err, spotify) ->
  # function body

And the same here:

spotify.get(uri, function(err, track){

Which should be:

spotify.get uri, (err, track) ->

CoffeeScript's function syntax is

(arguments...) ->
    body

and not

-> (arguments...) {
    body
}

You have the correct syntax too:

robot.respond /play (.*)/i, (message) ->
    uri = message.match[1]
    ....

Did you copy-paste a snippet from somewhere?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744723997a4590077.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信