2017-10-15 111 views
1

我正在關注This教程,嘗試將一些功能部署到Firebase,並且我可以將它們成功部署到我的項目ID。當訪問所提供的URL下面的函數,我得到的錯誤:無法處理請求使用Firebase雲功能部署/測試功能

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup 
triggers. 
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.date = functions.https.onRequest((req, res) => { 
let format = req.query.format; 
const date = moment().format(); 
res.status(200).json({ date: date}); 
}) 

我的火力地堡控制檯日誌本上執行:

ReferenceError: moment is not defined 
at exports.date.functions.https.onRequest (/user_code/index.js:10:24) 
at cloudFunction (/user_code/node_modules/firebase- 
functions/lib/providers/https.js:26:41) 
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在我functions文件夾中有"moment": "^2.19.1"依賴關係下。

我在做什麼錯?我如何定義moment

回答

2

在您的package.json中定義的moment.js僅表示它將安裝在代碼運行的環境中。有它在你的代碼中,你需要導入moment庫:

const functions = require('firebase-functions'); 
const moment = require('moment'); 

const admin = require ('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 

exports.date = functions.https.onRequest((req, res) => { 
    let format = req.query.format; 
    const date = moment().format(); 
    res.status(200).json({ date: date}); 
}) 

另見moment.js docs for using it in node.js

+0

當然..謝謝:) –