【问题标题】:TypeError: Cannot assign to read only property '_msgid'TypeError:无法分配给只读属性“_msgid”
【发布时间】:2016-05-10 10:08:08
【问题描述】:

我正在尝试使用 node-red...

我做这个网络查询https://api.forecast.io/forecast/

这是给定的输出:

{"latitude":-25.74486,"longitude":28.18783,"timezone":"Africa/Johannesburg","offset":2,"currently":
{"time":1462871342,"summary":"Foggy","icon":"fog","precipIntensity":0,"precipProbability":0,
"temperature":18.08,"apparentTemperature":18.08,
"dewPoint":9.3,"humidity":0.57,"windSpeed":3.75,"windBearing":351,"visibility":2.78,
"cloudCover":0.23,"pressure":1024.87,"ozone":252.43}}

这似乎是正确的。

Parse Weather 中的代码是

  //parse forecast.io message

 var weather = JSON.parse(msg.payload);

 return weather.currently.temperature;

我得到的错误是:

TypeError:无法分配给 19.49 的只读属性“_msgid”

这个值似乎还可以。

我做错了什么?

提前致谢。

【问题讨论】:

  • 我在您的 JSON 中没有看到任何名为“_msgid”的属性。有的话能不能也给个JS代码?

标签: node-red


【解决方案1】:

问题出在您的 Parse Weather 函数中。您正在返回一个 int (18.08),Function 节点需要返回一个 msg 对象。

试试这样的:

var weather = JSON.parse(msg.payload);
msg.payload = weather.currently.temperature;

return msg;

【讨论】:

  • 感谢您的回答。
  • @NicovdDussen 如果此答案有帮助,您应该将其标记为已接受。我注意到此代码块与您在自己的答案中发布的相同。
【解决方案2】:

找到虚拟错误。

将功能代码改为:

//parse forecast.io message

 var weather = JSON.parse(msg.payload);
 msg.payload = weather.currently.temperature;
 return msg;

现在一切都在按预期工作:)

【讨论】:

    猜你喜欢
    • 2019-12-06
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多