我尝试通过以下教程来实现firebase函数:https://firebase.google.com/docs/functions/get-started
下面是我的代码。当我尝试调用网址https://us-central1-skoon-5ed4d.cloudfunctions.net/addMessage时,出现“错误:无法处理请求”
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.addMessage = functions.https.onRequest(async (req, res) => {
const original = req.query.text;
const snapshot = await admin.database().ref('/messages').push({original: original});
Firebase console.res.redirect(303, snapshot.ref.toString());
});
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
const original = snapshot.val();
console.log('Uppercasing', context.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return snapshot.ref.parent.child('uppercase').set(uppercase);
});
我删除了异步并等待,并且在日志中出现错误:
Reference.push failed: first argument contains undefined in property 'messages.original'
at validateFirebaseData (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1433:15)
at /srv/node_modules/@firebase/database/dist/index.node.cjs.js:1479:13
at Object.forEach (/srv/node_modules/@firebase/util/dist/index.node.cjs.js:837:13)
at validateFirebaseData (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1462:14)
at validateFirebaseDataArg (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1421:5)
at Reference.push (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:14087:9)
at exports.addMessage.functions.https.onRequest (/srv/index.js:23:54)
at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /worker/worker.js:724:7
at /worker/worker.js:707:11