Javascript 在Nodejs中设置参数之前是否可以调用函数?
我有以下代码:Javascript 在Nodejs中设置参数之前是否可以调用函数?,javascript,node.js,http,Javascript,Node.js,Http,我有以下代码: app.get('/lights/:id', (req,res) => { const state = req.query.state; const id = req.params.id; hue.setLightState(id, state, function(callback){ console.log(callback); res.send(callback); }) }); 它应该根据调用的URL和
app.get('/lights/:id', (req,res) => {
const state = req.query.state;
const id = req.params.id;
hue.setLightState(id, state, function(callback){
console.log(callback);
res.send(callback);
})
});
它应该根据调用的URL和查询参数通过ID设置飞利浦色调灯的状态,但是当Iconsole.log
每个变量(state
和ID
)根据URL中给出的内容输出正确的值时,它什么也不做,所以它们没有错
我知道当我自己输入值时,不是函数设置灯光故障,因为它工作(它打开ID为1的灯光):
所以我的假设是,hue.setLightState
函数在两个变量state
和id
被赋值之前被调用,因为没有其他解释,是吗
编辑(请求时附加代码):
App.js文件:
const express = require('express');
const app = express();
const hue = require('./hue');
app.post('/lights/:id', (req,res) => {
const state = req.query.state;
const id = req.params.id;
hue.setLightState(parseInt(id), state, function(callback){
console.log(callback);
res.send(callback);
})
});
app.listen(4000, () => {console.log("Server has started")});
Hue.js文件:
const fetch = require('node-fetch');
const gateway = "192.168.0.12";
const username = "username-A";
let getLights = function(callback){
fetch(`http://${gateway}/api/${username}/lights`, {
method: 'GET'
}).then((res) => {
return res.json();
}).then((json) => {
lightsArray = []
for (var i in json){
lightsArray.push(`ID: ${i} Name: ${json[i]['name']}`);
}
return callback(lightsArray);
});
}
let getLightDetails = function (ID, callback) {
fetch(`http://${gateway}/api/${username}/lights/${ID}`, {
method: 'GET'
}).then((res) => {
if(res.ok){
return res.json();
}else{
throw new Error(res.message);
}
}).then((json) => {
return callback(json);
})
.catch((err) => {
console.log(err);
return callback(err.message);
});
}
let getLightState = function(ID, callback) {
fetch(`http://${gateway}/api/${username}/lights/${ID}`, {
method: 'GET'
}).then((res) => {
return res.json();
}).then((json) => {
return callback(`Name: ${json['name']} On: ${json['state']['on']}`);
});
}
let setLightState = function (ID, state, callback) {
fetch(`http://${gateway}/api/${username}/lights/${ID}/state`, {
method: 'PUT',
body: JSON.stringify({"on":state})
}).then((res) => {
return callback(res);
})
}
module.exports = {getLights, getLightDetails, getLightState, setLightState};
正如我所说的,这些函数可以工作,所以Hue.js文件不会是问题所在,但它仍然存在
用于调试的布尔开关案例:
switch (state){
case true || 'true':
console.log('its set to true');
break;
case false || 'false':
console.log('its set to false');
break;
}
始终输出设置为true的
比较有效的代码:
hue.setLightState(1, true, function(callback){
还有一个没有:
hue.setLightState(parseInt(id), state, function(callback){
唯一的区别在于状态
变量的转换。您应该将其转换为布尔值
转换id
变量是无害的,因为在构建URL时会将其转换回字符串:
fetch(`http://${gateway}/api/${username}/lights/${ID}/state`, {
但是状态
变量将被序列化为JSON:
body: JSON.stringify({"on":state})
因此,您的身体可能被提交为:
{"on":"true"}
何时应该:
{"on":true}
“没有其他解释,是吗?”必须有另一种解释,因为这是不可能的。同步设置变量,并立即使用这些变量调用hue.setLightState
。就像你做了console.log
“When I console.log state”哪个?内部还是外部?@JaredSmith,确保任何异步调用都不会影响输出确保尊重函数的签名,也许就是这样。如果不是一,则将id
转换为一个数字;如果不是一,则将state
转换为布尔值。当应用程序初始化时(例如,当您的路线被声明时),而不是在您正在运行的express应用程序中,您的hue.setLightState
是否可以正常工作?我发誓这是为了骗我?!我讨厌JS哈哈,虽然这部分解决了我的问题-我只能用它来设置灯为开(真),我不能做相反的事情,并设置为假(关)。为什么?你应该检查你的布尔转换。我建议写一个自定义的,类似这样的(伪代码):switch(value){case'true','true','T',1','1','on':return true;default:false}
是的,我甚至重复了一遍“我确信你没有为状态传递布尔值”,并且@Nathan回答说“我尝试了所有这些组合,但仍然不起作用。”@Nathan,请用您的布尔转换逻辑更新问题,然后。@Nathanhue.setLightState(id,state==“true”,function…
,您就完成了
{"on":true}