封装的API.对外部javascript函数的调用不工作
Encapsulating API.ai call into an external javascript function is not working
代码运行正常:
var apiai = require('apiai');
var app = apiai(<token>);
var request = app.textRequest('jacket', {
'sessionId': 123456
});
request.on('response', function(response) {
console.log(response);
console.log(response.result.fulfillment.speech);
console.log(response.result.metadata.intentName);
});
request.on('error', function(error) {
console.log(error);
});
request.end();
并产生以下控制台输出:
{ id: '2865abde-ac8f-4ac3-816e-483c68f709d0',
timestamp: '2016-11-08T22:38:56.461Z',
result:
{ source: 'agent',
resolvedQuery: 'jacket',
action: '',
actionIncomplete: false,
parameters: {},
contexts: [ [Object] ],
metadata:
{ intentId: '357af94f-00b3-4016-a661-732419ddafd6',
webhookUsed: 'false',
intentName: 'clothing' },
fulfillment: { speech: 'clothing2', messages: [Object] },
score: 0.6 },
status: { code: 200, errorType: 'success' },
sessionId: '123456' }
clothing2
clothing
然而,当我试图将它分解成一个函数调用时,事情就不那么好了:
文件:main.js
var apiai = require('apiai');
var app3 = apiai(<token>);
exports.getresponse = function(inputstring){
var request = app3.textRequest(inputstring, {'sessionId': 123456});
request.on('response', function(response) {
return(response);
});
request.on('error', function(error) {
return(error);
});
request.end();
};
文件:moda.js
var express = require("express");
var app = express();
var joe = require('./main.js');
console.log(joe.getresponse('jacket'));
app.listen(3000);
当我运行moda.js (node moda.js)时,我只得到
undefined
作为控制台输出。
我在这里做错了什么?
非常感谢!
joe.getresponse()
没有显式返回,因此它有隐式return undefined;
这就是为什么你在控制台输出中看到undefined
。
原因有点复杂,这是javascript编程的基础,但它与回调和javascript的异步执行有关。
在这一行中,您实际上添加了一个匿名函数作为回调。该函数在被调用(通常是在响应事件时)之前实际上不会执行。
request.on('response', function(response) {
return(response);
});
因此,当您返回响应时,该返回的范围不在getresponse()
外部函数内,而是在response
事件发射器内。
相反,您需要以一种可以确定地访问它的方式捕获响应值。通过向getresponse()
方法提供您自己的回调,该回调将能够访问响应对象。
它看起来像这样:
main.js
var apiai = require('apiai');
var app3 = apiai(<token>);
exports.getresponse = function(inputstring,callback){
var request = app3.textRequest(inputstring, {'sessionId': 123456});
request.on('response', function(response) {
callback(null,response);
});
request.on('error', function(error) {
callback(error,null);
});
request.end();
};
moda.js
var express = require("express");
var app = express();
var joe = require('./main.js');
joe.getresponse('jacket', function(error,response) {
if(error) { console.error(error); }
console.log(response);
});
app.listen(3000);
我没有返回响应值,而是调用调用方提供的回调,然后该回调处理响应(调用console.log)。
相关文章:
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么ng控制器不调用或工作或功能不工作
- 点击功能没有'ajax调用动态元素(Backbone)后无法工作
- Javascript作用域和Ajax调用;工作不正常
- 使用几个<脚本>标签不会't工作-只调用一个脚本
- 给出<选项>标记一个类?API调用不工作
- show-hide只在ajax调用中第一次工作
- 提交按钮只能在onchange调用输入文本后第二次点击时工作
- 表单提交没有'调用return false后无法工作
- Ajax调用PHP失败,ASP正常工作.NET
- Ajax调用工作正常,但php文件在从对象检索属性时出错
- Ajax调用工作缓慢.可以做得很快
- 为什么不't多个$.getJSON调用工作正常
- 如何让我的jQuery之前()调用工作
- AngularJS:拆分到新文件时不调用工作指令
- 为什么不't此方法从原型中调用工作
- MVC方法调用工作在提琴,但不是在浏览器
- 只有一个函数调用工作
- Javascript 井字 - 获胜函数未调用/工作
- Sencha touch 2不能让Ajax调用工作