2

我想用雲功能做一些測試以實現更大的目標。作爲一名Android開發人員,我的知識在JavaScript方面有點有限。Firebase未找到雲端功能?

我想從使用雲功能的Firebase訪問這個數據庫。

Firebase Realtime Database

我來訪問該數據庫的代碼來獲得在瀏覽器中JSON響應。

const functions = require('firebase-functions'); 
 
    const admin = require('firebase-admin'); 
 
    admin.initializeApp(functions.config().firebase); 
 
    const cors = require('cors')({origin: true}); 
 
    
 
    // Take the text parameter passed to this HTTP endpoint and insert it into the 
 
    // Realtime Database under the path /messages/:pushId/original 
 
    exports.addMessage = functions.https.onRequest((req, res) => { 
 
     // Grab the text parameter. 
 
     const original = req.query.text; 
 
     // Push the new message into the Realtime Database using the Firebase Admin SDK. 
 
     admin.database().ref('/messages').push({original: original}).then(snapshot => { 
 
     // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console. 
 
     res.redirect(303, snapshot.ref); 
 
     }); 
 
    }); 
 
    
 
    // Listens for new messages added to /messages/:pushId/original and creates an 
 
    // uppercase version of the message to /messages/:pushId/uppercase 
 
    exports.makeUppercase = functions.database.ref('/messages/{pushId}/original') 
 
     .onWrite(event => { 
 
      // Grab the current value of what was written to the Realtime Database. 
 
      const original = event.data.val(); 
 
      console.log('Uppercasing', event.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 event.data.ref.parent.child('uppercase').set(uppercase); 
 
     }); 
 
    
 
    var db = admin.database(); 
 
    exports.getUserMessage = functions.https.onRequest((req, res) => { 
 
    var query = firebase.database().ref("messages").orderByKey(); 
 
    query.once("value") 
 
     .then(function(snapshot) { 
 
     snapshot.forEach(function(childSnapshot) { 
 
      // key will be "ada" the first time and "alan" the second time 
 
      var key = childSnapshot.key; 
 
      // childData will be the actual contents of the child 
 
      var childData = childSnapshot.val(); 
 
     }); 
 
    }); 
 
    });

但我得到的錯誤:

Error: could not handle the request

這些都是我的登錄錯誤從火力地堡:

ReferenceError: firebase is not defined 
    at exports.getUserMessage.functions.https.onRequest (/user_code/index.js:34:49) 
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:26:47) 
    at /var/tmp/worker/worker.js:635:7 
    at /var/tmp/worker/worker.js:619:9 
    at _combinedTickCallback (internal/process/next_tick.js:73:7) 
    at process._tickDomainCallback (internal/process/next_tick.js:128:9) 

的package.json腳本

{ 
    "name": "functions", 
    "description": "Cloud Functions for Firebase", 
    "dependencies": { 
    "cors": "^2.8.1", 
    "firebase-admin": "~4.2.1", 
    "firebase-functions": "^0.5.7" 

    }, 
    "private": true 
} 

回答

4

這裏是那裏的問題是:

var query = firebase.database().ref("messages").orderByKey(); 

應該

db.ref("messages").orderByKey(); 

firebase你在該行使用,否則無法進行火力地堡雲功能的定義。如果你想查詢數據庫,你已經有一個使用管理SDK的引用:

var db = admin.database(); 
+0

糟糕,我已經更新了代碼。應該說'ref',而不是'child'。如果您不確定哪些功能可用,請查看[文檔](https://firebase.google.com/docs/reference/admin/node/admin.database.Reference)。 –

+0

通過實施您的修復,我能夠解決以前的錯誤,但現在我得到'功能執行耗時60003毫秒,完成狀態:'超時'錯誤 –

+0

[代碼](https://pastebin.com/XCNKejq1)更新但獲得超時錯誤 –