Node.js 我正在尝试部署一个云函数,并出现以下错误“无法读取未定义的状态”
我正在尝试编写一个函数,将数据从firestore发送到GoogleSheet。以下是我在index.js中的函数:Node.js 我正在尝试部署一个云函数,并出现以下错误“无法读取未定义的状态”,node.js,google-cloud-firestore,google-cloud-functions,Node.js,Google Cloud Firestore,Google Cloud Functions,我正在尝试编写一个函数,将数据从firestore发送到GoogleSheet。以下是我在index.js中的函数: const {google} = require('googleapis'); const { promisify } = require('util'); exports.loadInformation = functions.firestore.document('incident-report/{id}').onCreate((err, req, res, next) =&
const {google} = require('googleapis');
const { promisify } = require('util');
exports.loadInformation = functions.firestore.document('incident-report/{id}').onCreate((err, req, res, next) => {
console.log('I am triggered')
google.auth.getClient({
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
}).then(auth => {
const api = google.sheets({ version: 'v4', auth });
const getSheets = promisify(api.spreadsheets.get.bind(api.spreadsheets));
return getSheets({ spreadsheetId: '1hCF8jDt6uqYZ7qC93To2n0MbGzDWPIBU72IMp2xqh5Y' });
})
// This just prints out all Worksheet names as an example
.then(({ data: { sheets } }) => {
res.status(200).send({ sheets });
})
.catch(err => {
res.status(500).send({ err });
})
});
我得到以下错误。
TypeError:无法读取未定义的属性“status”
我怎样才能解决这个问题?是我的函数看起来不错。Firestore onCreate触发器需要一个具有两个参数而不是四个参数的处理函数。它抱怨res未定义的原因是,由于它是只传递两个DocumentSnapshot和EventContext的函数中的第三个参数,因此该参数没有传递任何内容。阅读这些文档,它会引导你走上正确的轨道。还请注意,没有一个示例使用常见的错误优先回调样式,例如err,…=> Firestore onCreate触发器需要一个具有两个参数而不是四个参数的处理函数。它抱怨res未定义的原因是,由于它是只传递两个DocumentSnapshot和EventContext的函数中的第三个参数,因此该参数没有传递任何内容。阅读这些文档,它会引导你走上正确的轨道。还请注意,没有一个示例使用常见的错误优先回调样式,例如err,…=> 使用两个参数调用Firestore的onCreate函数 代码预期的参数不正确。 同时删除res.status200.send。此部分用于anHTTPS函数和notFirestore onCreate触发器 更改如下:
const { google } = require('googleapis');
const { promisify } = require('util');
exports.loadInformation = functions.firestore.document('incident-report/{id}').onCreate((doc, context) => {
console.log('I am triggered')
return google.auth.getClient({
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
}).then(auth => {
const api = google.sheets({ version: 'v4', auth });
const getSheets = promisify(api.spreadsheets.get.bind(api.spreadsheets));
return getSheets({ spreadsheetId: '1hCF8jDt6uqYZ7qC93To2n0MbGzDWPIBU72IMp2xqh5Y' });
})
.catch(err => {
console.error(err);
});
});
使用2个参数调用Firestore的onCreate函数 代码预期的参数不正确。 同时删除res.status200.send。此部分用于anHTTPS函数和notFirestore onCreate触发器 更改如下:
const { google } = require('googleapis');
const { promisify } = require('util');
exports.loadInformation = functions.firestore.document('incident-report/{id}').onCreate((doc, context) => {
console.log('I am triggered')
return google.auth.getClient({
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
}).then(auth => {
const api = google.sheets({ version: 'v4', auth });
const getSheets = promisify(api.spreadsheets.get.bind(api.spreadsheets));
return getSheets({ spreadsheetId: '1hCF8jDt6uqYZ7qC93To2n0MbGzDWPIBU72IMp2xqh5Y' });
})
.catch(err => {
console.error(err);
});
});
尝试先检查err变量,它可能有一条消息出错尝试先检查err变量,它可能有一条消息出错