Can';t获取CoffeeScript以识别js文件中的函数
我正在用Coffeescript编写一个简单的应用程序来控制飞利浦色调灯。我已将其纳入我的项目中。在我尝试使用Can';t获取CoffeeScript以识别js文件中的函数,coffeescript,philips-hue,framerjs,Coffeescript,Philips Hue,Framerjs,我正在用Coffeescript编写一个简单的应用程序来控制飞利浦色调灯。我已将其纳入我的项目中。在我尝试使用setLightState设置灯光颜色之前,下面的代码似乎工作正常。编译器说这个函数不是一个函数。我不太明白为什么它不能识别这个功能 # Connect with Philips Hue bridge jsHue = require 'jshue' hue = jsHue() # Get the IP address of any bridges on the network hueB
setLightState
设置灯光颜色之前,下面的代码似乎工作正常。编译器说这个函数不是一个函数。我不太明白为什么它不能识别这个功能
# Connect with Philips Hue bridge
jsHue = require 'jshue'
hue = jsHue()
# Get the IP address of any bridges on the network
hueBridgeIPAddress = hue.discover().then((bridges) =>
if bridges.length is 0
console.log 'No bridges found. :('
else
(b.internalipaddress for b in bridges)
).catch((e) => console.log 'Error finding bridges', e)
if hueBridgeIPAddress.length isnt 0 # if there is at least 1 bridge
bridge = hue.bridge(hueBridgeIPAddress[0]) #get the first bridge in the list
# Create a user on that bridge (may need to press the reset button on the bridge before starting the tech buck)
user = bridge.createUser('myApp#testdevice').then((data) =>
username = data[0].success.username
console.log 'New username:', username
bridge.user(username)
)
if user?
#assuming a user was sucessfully created set the lighting to white
user.setLightState(1, {on:true, sat:0, bri:100, hue:65280}).then((data) =>
# process response data, do other things
)
正如您在桥上看到的,bridge.createUser
不会直接返回user
对象
相反,示例代码在返回的函数的中设置user
变量,然后设置:
可以预期,使用这种方法,user
变量将被正确设置,并且将定义user.setLightState
一个独立的例子:
例如:
url = "https://api.ipify.org?format=json"
outside = axios.get(url).then (response) =>
inside = response.data.ip
console.log "inside: #{inside}"
inside
console.log "outside: #{outside}"
控制台输出为
outside: [object Promise]
inside: 178.19.212.102
你可以看到:
外部
日志是第一个,是一个承诺
对象
内部的日志是最后一个日志,包含来自Ajax调用的实际对象(在本例中是您的IP)
then
函数在
内隐式返回,不会更改任何内容
请提供完整的错误信息。这正是我在问题中所说的。它说setLightState不是一个函数。好吧,祝你好运解决你的问题。问题是,使用Coffeescript,函数的最后一行bridge。返回user(username)
,并设置为user
,因此我的代码有效地执行了你的建议。。。。或者至少应该是这样。@cHorse不是真的bridge.createUser
立即返回(一个Promise
对象)。在某些内部机制(可能是Ajax调用)成功后,Promise将调用传递给它的然后方法的函数,并将来自Ajax调用的数据作为参数。传递给Promise的then
方法的函数隐式返回它的最后一个表达式(因为CoffeeScript)没有帮助,该函数的返回值将被丢弃。事实上,整个问题根本与咖啡脚本无关。我想我明白你的意思了createUser
需要一些时间才能返回。在这过程中,我的代码尝试对尚未初始化的对象调用setLightState
。因此出现了“非函数”错误。更新后添加了精简示例。
outside: [object Promise]
inside: 178.19.212.102