为什么数据库快照未定义 Firebase 管理节点 Js



以下代码直接来自Firebase Functions入门教程:

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

感谢@cartan为我指出正确的方向

原始示例从 Firebase 数据库中"读取",因此返回了一个快照商店值

另一方面,我的代码尝试"写入"Firebase数据库,没有返回任何值,而是回调。
我已将代码更改为以下内容,并且一切都按预期完美运行:

admin.database().ref(dbPath).update({dummy: dummyJson}, function (err) {
    console.log("done with error " + err);
    res.status(200).send("success");
  });

最新更新