2017-04-24 42 views
0

我已经加入请求模块和由在主settings.js变化每文档中的功能的节点:无法从请求包访问get方法在

// Anything in this hash is globally available to all functions. 
    // It is accessed as context.global. 
    // eg: 
    // functionGlobalContext: { os:require('os') } 
    // can be accessed in a function block as: 
    // context.global.os 

    functionGlobalContext: { 
    requestModule:require('request').defaults({jar: true}) 
     // os:require('os'), 
     // octalbonescript:require('octalbonescript'), 
     // jfive:require("johnny-five"), 
     // j5board:require("johnny-five").Board({repl:false}) 
    }, 

这是代码在我的功能节点中的一个:

var request = context.global.requestModule; 
request.get('http://192.168.0.44:3000' + '/api/stats', 
function (err, data) { 
    if (err) { 
     console.log("could not process data"); 
    } 
    else { 
     var body = JSON.parse(data.body); 
     msg.payload = body.deviceCount; 
    } 
}); 
return msg; 

我的节点红色版本(0.16.2)

+0

为什么你需要使用请求模块呢? http请求节点将正常工作 – hardillb

+0

我需要对每个数据访问请求进行一些额外的身份验证过程,该请求在默认的http请求节点中不存在 –

回答

0

,因为你的异步调用回到请求对象调用之前返回msg这是行不通的。以下应该工作:

var request = context.global.requestModule; 
request.get('http://192.168.0.44:3000' + '/api/stats', 
function (err, data) { 
    if (err) { 
     console.log("could not process data"); 
    } 
    else { 
     var body = JSON.parse(data.body); 
     msg.payload = body.deviceCount; 
     node.send(msg) 
    } 
}); 
+0

感谢您的更正;但request.get仍然不能从context.global.requestModule中识别出我是否错过了一些相关的东西? –