我试图制作一个Node-Red应用程序。它可能看起来很愚蠢,但它实际上是一个有趣的例子,我需要做的事情是幽默的。 HTTP输入触发对Urban Dictionary API的HTTP请求,并刮除术语" Honey Badger"的随机定义。
然后一个函数将字符串咀嚼成片并拉出实际定义并丢弃所有元垃圾。
然后将String传递给模板,该模板在{{payload}}行之前和之后包含HTML加载,以生成显示Urban Dictionary中内容的页面,并在每个页面加载上显示随机定义。
我得到了我想要的所有东西,调试我的模板的输出显示了日志中的UD内容但是在浏览器中我得到的是[object OBJECT]我的文本段落应该是。
据我所知,这是因为函数输出的是对象,而不是字符串。
我并不完全了解对象是什么。
我从来没有编过任何JS,这是我从哪里开始的。
Ext.application({
name : 'Fiddle',
launch : function() {
Ext.Viewport.add({
xtype: 'grid',
itemConfig: {
viewModel: true
},
store: {
data: [{
name: 'A',
fav: false
}, {
name: 'B',
fav: true
}]
},
columns: [{
dataIndex: 'name'
}, {
text: 'Favorite',
width: 80,
align: 'center',
cell: {
tools: {
play : {
bind: {
iconCls:'x-fa yellow {record.fav ? "fa-star" : "fa-star-o"}',
},
align:'center',
tooltip : 'Toggle Favorites',
handler: function(grid, context) {
var r = context.record;
r.set('fav', !r.get('fav'));
}
}
}
}
}]
});
}
});
答案 0 :(得分:1)
首先,您需要删除http-in
和http-response
节点之间的直接链接,因为这只会向发出请求的浏览器返回任何内容。
接下来的问题是,要使http-in / http-response节点正常工作,您需要将http-in
节点生成的原始消息一直传递到http-response
节点。您正在msgsparts1
函数节点中生成新消息(Test
)对象,这将无效。
来自都市词典的响应也是一个JSON对象,所以不要尝试使用String函数.split()来解决它,而是更容易解析JSON并只访问它的字段。您可以使用JSON node
解析字符串和change node
将描述移动到msg.payload来执行此操作。
在将邮件发送到http-request
之前,您还需要从http-response
删除标头。您可以使用相同的change node
[{"id":"72cc29ed.aa819","type":"http in","z":"8aa94366.b8bde","name":"badger","url":"/Badger","method":"get","swaggerDoc":"","x":90,"y":160,"wires":[["2f9e6174.bde466"]]},{"id":"2f9e6174.bde466","type":"http request","z":"8aa94366.b8bde","name":"URBAN DICTIONARY","method":"GET","ret":"txt","url":"https://urban.now.sh/random/Honey Badger","tls":"","x":200,"y":260,"wires":[["5d7d10da.a757d"]]},{"id":"c6b86250.24357","type":"http response","z":"8aa94366.b8bde","name":"http response","x":960,"y":220,"wires":[]},{"id":"5d1bd9d2.9efad8","type":"template","z":"8aa94366.b8bde","name":"Template","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE html>\n<HTML>\n\n\t<HEAD>\n\t\t<TITLE> [ H O N E Y B A D G E R ] </TITLE>\n\t\t<META HTTP-EQUIV=\"refresh\" CONTENT=\"300\">\n<style>\np.minortext\n{\n font-family: \"Arimo\";\n color:#A9A9A9\n}\np.majortext\n{\n font-family: \"Impact\";\n color:black\n size:20\n}\n\n</style>\n</head>\n\n\t<BODY>\n\t <CENTER>\n\t <Table Width = \"100%\" Bgcolor=\"#A9A9A9\"><TR><TD ALIGN = \"CENTER\">\n\t <a class=\"weatherwidget-io\" href=\"https://forecast7.com/en/n34d93138d60/adelaide/\" data-font=\"Roboto Slab\" data-mode=\"Current\" data-days=\"3\" data-shadow=\"#000000\" data-accent=\"#000000\" data-textColor=\"#ffffff\" data-highColor=\"#ffffff\" data-lowColor=\"#ffffff\" data-sunColor=\"#ffffff\" data-moonColor=\"#ffffff\" data-cloudColor=\"#ffffff\" data-cloudFill=\"#ffffff\" data-rainColor=\"#ffffff\" data-snowColor=\"#ffffff\" >Adelaide, South Australia, Australia</a>\n<script>\n!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=\"https://weatherwidget.io/js/widget.min.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"weatherwidget-io-js\");\n</script>\n</td><td align = \"center\">\n\n\n</td>\n\n\n\n</TD><TD ALIGN = \"RIGHT\"><IMG SRC=\"LogIn.PNG\"> </TD></TR></Table>\n\t <Table Width = \"500\" Height = \"620\" Border = \"0\">\n\t <TR Width = \"500\" Height = \"320\">\n\t <TD Width = \"500\" Height = \"320\" VALIGN = \"CENTER\">\n\t\t<CENTER>\n\t\t\t <FONT SIZE = \"20\">\n <p class=\"majortext\">\n <IMG SRC=\"HoneyBadger.PNG \" ALT=\"HoneyBadger\"><BR>\n\t\t\t\t <B>H O N E Y B A D G E R</B>\n\t\t\t </P>\n\t\t\t </FONT>\n\t\t\t </TD>\n\t\t\t </TR>\n\t\t\t \t <TR Width = \"500\" Height = \"300\">\n\t <TD Width = \"500\" Height = \"300\" VALIGN = \"TOP\">\n\t\t\t<CENTER>\n\t\t\n\t\t\t<p class=\"minortext\">\n\t\t\t \t\t\t <FONT SIZE = \"5\">\n\t\t\t {{{payload}}}\n </P>\n\t\t</CENTER>\n\t\t </TD>\n\t\t </TR>\n\t\t </TABLE>\n\t\t</CENTER>\n\t</BODY>\n</HTML>\t\t\t \n\n","x":780,"y":260,"wires":[["bc66ae55.037f78","c6b86250.24357"]]},{"id":"bc66ae55.037f78","type":"debug","z":"8aa94366.b8bde","name":"","active":true,"console":"true","complete":"payload","x":940,"y":300,"wires":[]},{"id":"5d7d10da.a757d","type":"json","z":"8aa94366.b8bde","name":"","x":390,"y":260,"wires":[["8ecda72f.12eed8"]]},{"id":"8ecda72f.12eed8","type":"change","z":"8aa94366.b8bde","name":"","rules":[{"t":"move","p":"payload.definition","pt":"msg","to":"payload","tot":"msg"},{"t":"delete","p":"headers","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":260,"wires":[["e0951766.77e858","5d1bd9d2.9efad8"]]},{"id":"e0951766.77e858","type":"debug","z":"8aa94366.b8bde","name":"","active":true,"console":"false","complete":"false","x":810,"y":180,"wires":[]}]