前导小数点可能与点混淆

问题描述 投票:1回答:2

我尝试通过节点红色获取地下天气的一些信息。我发出了一个HTTP请求,要求每天有十天一小时的天气。

我需要提取一个临时值。我用这个:

var newMsg = { payload: msg.payload.hourly_forecast.0.temp.metric };
return newMsg;

如果我将它放入调试控制台,msg.payload.hourly_forecast.0.temp.metric可以工作,但在我的函数中,我有这个错误:

前导小数点可以与点混淆:'。0'

所以我试试这个:

var toto = 0;
var newMsg = { payload: msg.payload.hourly_forecast + '.' + toto + '.' + temp.metric };
return newMsg;

但不工作,错误是

ReferenceError:未定义temp(第2行,第74行)

enter image description here

javascript node.js function node-red
2个回答
3
投票

msg.payload.hourly_forecast是你在这里展示的阵列https://ibb.co/ejaxge

请使用msg.payload.hourly_forecast[0].temp.metric

在这里阅读更多关于数组:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array


1
投票

要在JavaScript中处理数组元素,请使用方括号表示法:

var newMsg = { payload: msg.payload.hourly_forecast[0].temp.metric };
return newMsg;

Node-RED中的Debug侧栏也可用于标识任何消息元素的路径。这在文档中描述:https://nodered.org/docs/user-guide/messages#understanding-the-structure-of-a-message

© www.soinside.com 2019 - 2024. All rights reserved.