以下代码直接Firebase Functions getting started tutorial:
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.addMessage = functions.https.onRequest((req, res) => {
let userid = 'afewfaewf';
let dummyJson = {key: 'value'};
let dbPath = '/' + userid;
admin.database().ref(dbPath).update({dummy: dummyJson}).then(snapshot => {
console.log('finished writing to Firebase database ');
console.log(snapshot === undefined);
res.redirect(303, snapshot.ref);
});
});
以下是本地运行它的完整输出:
❯ firebase serve --only functions
=== Serving from '/Users/eric/AndroidStudioProjects/XXX/firebase-functions'...
i functions: Preparing to emulate HTTPS functions. Support for other event types coming soon.
Warning: You're using Node.js v7.7.1 but Google Cloud Functions only supports v6.9.1.
Server#addProtoService is deprecated. Use addService instead
✔ functions: addMessage: http://localhost:5002/xxx/us-central1/addMessage
info: User function triggered, starting execution
info: finished writing to Firebase database
info: true
error: (node:11005) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'ref' of undefined
(node:11005) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
在云端:
11:44:06.290 PM
warning
addMessage
TypeError: Cannot read property 'ref' of undefined at admin.database.ref.update.then.snapshot (/user_code/index.js:32:31) at process._tickDomainCallback (internal/process/next_tick.js:129:7)
TypeError: Cannot read property 'ref' of undefined
at admin.database.ref.update.then.snapshot (/user_code/index.js:32:31)
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
11:44:06.289 PM
warning
addMessage
Unhandled rejection
11:44:06.288 PM
info
addMessage
true
11:44:06.283 PM
info
addMessage
finished writing to Firebase database
11:44:05.322 PM
outlined_flag
addMessage
Function execution started
答案 0 :(得分:0)
感谢@cartan指出我正确的方向
原始示例"读取"来自Firebase数据库,因此返回了snapshop值
我的代码在另一方尝试"写"到Firebase数据库,没有返回任何值,而是回拨 我已将代码更改为以下内容,所有内容都按预期完美运行:
admin.database().ref(dbPath).update({dummy: dummyJson}, function (err) {
console.log("done with error " + err);
res.status(200).send("success");
});