我正在使用 Express 运行 Node.js 服务器来与 Actions on Google 交互。为了满足请求,我需要访问外部服务器,因此我使用 Fetch 库。我似乎无法让 Node 服务器等待 Web 服务调用给出适当的响应。我尝试过使用 Promises 和 Await/Async 但我没有正确实现它..有人可以帮助完成这项工作吗?谢谢!
let fetch = require('node-fetch');
let express = require('express');
let bodyParser = require('body-parser');
let { dialogflow, Image } = require('actions-on-google');
let app = dialogflow();
let json;
function middleware(req,res,next) {
json = req.body;
next();
}
app.intent('StartIntent', conv => async function() {
const response = await communicate(json);
console.log(response);
// Not running synchronously
conv.ask('Need the response from above');
});
function communicate( message ) {
let response;
try {
response = fetch('https://stackoverflow.com', {
method: 'POST',
body: JSON.stringify(message),
headers: {
'Content-Type': 'application/json'
},
});
return response;
} catch(e) {
console.log(e);
return null;
}
}
express().use(bodyParser.json(), middleware, app).listen(3000);
@Prisoner 是正确的 - 仅当返回 communications() 结果中的 json 数据后才应调用 conv.ask()。然而,此时 Promise 对象中唯一返回的东西。数据被正确检索为 json 对象。
let fetch = require('node-fetch');
let express = require('express');
let bodyParser = require('body-parser');
let { dialogflow, Image } = require('actions-on-google');
let app = dialogflow();
let json;
function middleware(req,res,next) {
json = req.body;
next();
}
app.intent('StartIntent', conv => {
const response = communicate(json);
console.log("StartIntent Response:");
console.log(response); // output: Promise { <pending> }
// Not running synchronously :(
console.log(response.text);
conv.ask('Need the response from above');
});
function communicate( message ) {
let response;
try {
response = fetch('https://google.com', {
method: 'POST',
body: JSON.stringify(message),
headers: {
'Content-Type': 'application/json'
},
}).then(function(body) {
return body.json();
}).then(function(json) {
// The data is returning successfully
// For example: {"BTCUSD":10000}
return json;
}).catch(function() {
// error handling
});
return response; // This is returning a pending Promise
} catch(e) {
console.log(e);
return null;
}
}
express().use(bodyParser.json(), middleware, app).listen(3006);
最佳答案
fetch()
返回一个 Promise。您可以为 communicate()
链接 then()
调用,并在正文中使用 conv.ask()
的响应。
下面是一个简单的示例代码片段,使用 node-fetch 进行 API 调用并使用该调用的响应。 (请注意,此示例正在对 JSON 数据执行 GET 请求。)
'use strict';
const { dialogflow } = require('actions-on-google');
const functions = require('firebase-functions');
const fetch = require('node-fetch');
const app = dialogflow({debug: true});
// Default Welcome Intent
app.intent('Default Welcome Intent', (conv) => {
return apiCall().then((response) => {
conv.ask(`Response from above ${JSON.stringify(response)}`);
});
});
// Makes an API call to a URL to retrieve JSON data.
function apiCall() {
const URL = 'https://example.com/data.json';
return fetch(URL).then((response) => response.json());
}
exports.endpoint = functions.https.onRequest(app);
关于node.js - 使用 Node.js 和 Express for Action on Google 进行同步调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50725600/