node.js - 错误:没有为平台定义响应:列出用户谷歌日历事件时为空
问题描述
我正在尝试在节点 js 中实现一个提醒对话框流代理,以提醒用户在他的谷歌日历即将发生的事件上。但是,在调用列出即将发生的事件的意图时,我得到了一个没有为平台定义的响应:空错误。
这是我的代码:
const express = require('express');
const google = require('googleapis').google;
const jwt = require('jsonwebtoken');
const dfff = require('dialogflow-fulfillment')
const {googlec} = require('googleapis');
// Google's OAuth2 client
const OAuth2 = google.auth.OAuth2;
// Including our config file
const CONFIG = require('./config');
// Creating our express application
const app = express();
// Allowing ourselves to use cookies
const cookieParser = require('cookie-parser');
app.use(cookieParser());
// Setting up EJS Views
app.set('view engine', 'ejs');
app.set('views', __dirname);
console.log(dfff)
app.get('/', function (req, res) {
// Create an OAuth2 client object from the credentials in our config file
const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
// Obtain the google login link to which we'll send our users to give us access
const loginLink = oauth2Client.generateAuthUrl({
access_type: 'offline', // Indicates that we need to be able to access data continously without the user constantly giving us consent
scope: CONFIG.oauth2Credentials.scopes // Using the access scopes from our config file
});
return res.render("index", { loginLink: loginLink });
});
app.get('/auth_callback', function (req, res) {
// Create an OAuth2 client object from the credentials in our config file
const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
if (req.query.error) {
// The user did not give us permission.
return res.redirect('/error');
} else {
oauth2Client.getToken(req.query.code, function(err, token) {
if (err)
return res.redirect('/');
// Store the credentials given by google into a jsonwebtoken in a cookie called 'jwt'
res.cookie('jwt', jwt.sign(token, CONFIG.JWTsecret));
return res.redirect('/');
});
}
});
app.post('/', express.json(),(req,res)=>{
//if (!req.cookies.jwt) {
// We haven't logged in
//return res.redirect('/');
//}
const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
const calendar = google.calendar({version: 'v3' , auth:oauth2Client});
const agent = new dfff.WebhookClient({
request : req,
response : res
})
function welcome(agent){
agent.add("Hi")
}
function listEvents(agent){
calendar.events.list({
'calendarId': 'primary',
'auth':oauth2Client,
'timeMin': (new Date()).toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': 10,
'singleEvents': true,
'orderBy': 'startTime'
}).then((err,response)=> {
let events = response.result.items;
if (events.length > 0) {
for (let i = 0; i < events.length; i++) {
var event = events[i];
var when = event.start.dateTime;
if (!when) {
when = event.start.date;
}
return agent.add('Be ready for '+ event.summary + ' event '+ 'at ' + when )
}}
else {
return agent.add('You dont have any upcoming events.');
}
});
}
let intenMap = new Map()
intenMap.set('Default_Welcome_Intent',welcome)
intenMap.set('Remind_me',listEvents)
agent.handleRequest(intenMap)
});
// Listen on the port defined in the config file
app.listen(CONFIG.port, function () {
console.log(`Listening on port ${CONFIG.port}`);
});
每当处理 listEvents 函数时,我都会得到(错误:没有为平台 null 定义响应)知道为什么吗?
解决方案
问题在于,listEvents()
它执行了一个异步操作,该操作返回一个 Promise(对 的调用calendar.events.list()
),您可以通过该.then()
块处理该操作,但您无法让agent.handleRequest()
函数知道这一点并等待 Promise 完成。为此,您需要返回一个 Promise。
幸运的是,在你的情况下,解决方案很简单,因为调用和then()
链返回一个 Promise,你可以直接返回它。这可以通过添加return
关键字 before来完成calendar.events.list()
。它可能看起来像这样:
function listEvents(agent){
return calendar.events.list({
// Parameters go here
}).then((err,response)=> {
// Code to handle response and return a message go here
});
}
推荐阅读
- .htaccess - 将 http 重定向到 https,将 www 重定向到非 www 并在一个重定向中删除最后一个斜杠
- python - Pandas 将特定列转换为列表
- twain - 强制终止 WIA 或 TWAIN 驱动程序(扫描仪)
- angular - HttpInterceptor 在 Angular 6 中不起作用
- java - okhttp 中的代理问题
- sql - 有哪些带有图形查询设计器的 SQL 客户端..?
- angular - 使链中的第一个可观察对象成为可选的
- c# - 带静态类的循环引用项目
- python - (1064, "您的 SQL 语法有错误;.. 在第 1 行的 '%s, %s, %s, %s, %s)' 附近使用正确的语法")
- javascript - 如何在两者之间渲染 JSX 代码
标签