在 Google 上使用 Node.js 和 Express for Action 进行同步调用?
Make a synchronous call using Node.js and Express for Action on Google?
我是 运行 一个 Node.js 服务器,带有 Express 以与 Google 上的操作交互。为了完成请求,我需要访问外部服务器,所以我使用的是 Fetch 库。我似乎无法让节点服务器等待 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://whosebug.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 是正确的 - conv.ask() 只应在返回 communicate() 结果的 json 数据后调用。但是,此时唯一在 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()
returns 一个承诺。您可以链接 then()
对 communicate()
的调用,并在正文中使用对 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 服务器,带有 Express 以与 Google 上的操作交互。为了完成请求,我需要访问外部服务器,所以我使用的是 Fetch 库。我似乎无法让节点服务器等待 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://whosebug.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 是正确的 - conv.ask() 只应在返回 communicate() 结果的 json 数据后调用。但是,此时唯一在 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()
returns 一个承诺。您可以链接 then()
对 communicate()
的调用,并在正文中使用对 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);