2017-07-13 52 views
0

我正在用Node.js代碼調用API,但代碼始終引發錯誤。使用node.js調用api時出錯

基本上我創建微軟博特框架殭屍和投擲的圖像API和evalutaing響應

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) 

回答

0

這是因爲,rest.data確實不是一個功能。

請參閱該文檔:

這不是很清楚什麼是你想在這裏做,你爲什麼做這樣的說法,看你試圖解決這個問題。有了更多的上下文,給你一個更具體的答案會更容易,但是沒有足夠的信息,我可以告訴你的是閱讀你正在使用的模塊的文檔,或者使用其他可能更適合你的需求的模塊,比如請求,restler,unirest,superagent或Node中可用的其他HTTP/REST客戶端。