使用node.js调用api时出错

时间:2017-07-13 12:03:10

标签: javascript node.js rest

我正在使用以下Node.js代码调用API,但代码总是抛出错误。

基本上我正在使用Microsoft Bot Framework创建一个机器人并将图像投射到API并评估响应

    var restify = require('restify');
var builder = require('botbuilder');
var http = require("http");
var request= require("request");
var rest = require('rest');
var server = restify.createServer();

server.listen(process.env.port || process.env.PORT || 3978, function () { });
// Create the chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
    appId: 'APP_ID',
    appPassword: 'APP_PASS'
});


// Listen for messages from users 
server.post('/api/messages', connector.listen());

server.get('/', restify.plugins.serveStatic({
 directory: __dirname,
 default: '/index.html'
}));


var bot = new builder.UniversalBot(connector, function (session, results) {
  var attachment = session.message.attachments[0];
  request({url: attachment.contentUrl, encoding: null}, function (error, response, body) {
    // Take the image and post to the custom vision service
    rest.post('URL_OF_API' , {
      multipart: true,
      headers: {
        'Prediction-Key': 'KEY',
        'Content-Type': 'multipart/form-data'
      },
      data: {
        'filename': rest.data('TEST.png', 'image/png', body)
      }
    }).on('complete', function(data) {
      let men = 0.0; //not-men
      let women = 0.0; //not-women
      let topHit = { Probability: 0.0, Tag: '' }; 

      for (var i = 0; i < data.Predictions.length; i++) {
        if (data.Predictions[i].Tag === 'Men')
          men = data.Predictions[i].Probability;
        else if (data.Predictions[i].Tag === 'Women')
          women = data.Predictions[i].Probability;
        else {
          if (data.Predictions[i].Probability > topHit.Probability)
            topHit = data.Predictions[i];
        }
      }
      let gender = 'No idea';
      if (men > women)
        gender = 'Men';
      if (women > men)
        gender = 'Women';
    var opt='The Pic sent appears to be a'+gender+'or'+gender+'s cloth';
      session.endDialog(opt);
    }).on('error', function(err, response) {
      session.send('Error calling custom vision endpoint');
    }).on('fail', function(data, response) {
      session.send('Failure calling custom vision endpoint');
    });
  });
});

我收到此错误

'filename': rest.data('TEST.png', 'image/png', body)
                         ^

TypeError: rest.data is not a function
    at Request._callback (C:\Users\Tushar\botforarticle\app.js:39:26)
    at Request.self.callback (C:\Users\Tushar\botforarticle\node_modules\request\request.js:188:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (C:\Users\Tushar\botforarticle\node_modules\request\request.js:1171:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (C:\Users\Tushar\botforarticle\node_modules\request\request.js:1091:12)
    at IncomingMessage.g (events.js:291:16)
    at emitNone (events.js:91:20)

1 个答案:

答案 0 :(得分:0)

那是因为,rest.data确实不是一个功能。

参见文档:

目前还不是很清楚你在这里想做什么,为什么你这样做,以及你试图解决这个问题的原因。有了更多的上下文,你会更容易给出一个更具体的答案,但是如果没有足够的信息,我可以告诉你的是阅读你正在使用的模块的文档,或者使用一些更适合你需要的模块,比如请求,restler,unirest,superagent或Node中提供的其他HTTP / REST客户端。