无法让 CoffeeScript 识别 js 文件中的函数
Can't get CoffeeScript to recognize a function in a js file
我正在用 Coffeescript 编写一个简单的应用程序来控制飞利浦 Hue 灯。我已将 this module 包含到我的项目中。在我尝试使用 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
)
正如您在 github page of the jshue lib 上看到的那样,bridge.createUser
而不是 直接 return 一个 user
对象。
相反,示例代码在 returned promise 的 then
函数中设置 user
变量 :
bridge.createUser('myApp#testdevice').then(data => {
// extract bridge-generated username from returned data
var username = data[0].success.username;
// instantiate user object with username
var user = bridge.user(username);
user.setLightState( .... )
});
可以预期 - 使用这种方法 - user
变量将被正确设置并且 user.setLightState
将被定义。
一个self-contained例子:
以this Codepen为例:
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
你可以看到:
outside
日志是第一个 并且是一个Promise
对象
inside
日志最后 并包含来自 Ajax 调用的实际对象(在本例中为您的 IP)
then
函数隐式 returning inside
不会改变任何东西
我正在用 Coffeescript 编写一个简单的应用程序来控制飞利浦 Hue 灯。我已将 this module 包含到我的项目中。在我尝试使用 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
)
正如您在 github page of the jshue lib 上看到的那样,bridge.createUser
而不是 直接 return 一个 user
对象。
相反,示例代码在 returned promise 的 then
函数中设置 user
变量 :
bridge.createUser('myApp#testdevice').then(data => {
// extract bridge-generated username from returned data
var username = data[0].success.username;
// instantiate user object with username
var user = bridge.user(username);
user.setLightState( .... )
});
可以预期 - 使用这种方法 - user
变量将被正确设置并且 user.setLightState
将被定义。
一个self-contained例子:
以this Codepen为例:
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
你可以看到:
outside
日志是第一个 并且是一个Promise
对象inside
日志最后 并包含来自 Ajax 调用的实际对象(在本例中为您的 IP)then
函数隐式 returninginside
不会改变任何东西