use*_*377 11 node.js firebase google-cloud-firestore
当我遵循firebase的文档时,但无法连接到firebase。
有人面对这个问题吗?
https://firebase.google.com/docs/firestore/quickstart
https://firebase.google.com/docs/firestore/manage-data/add-data
index.js
var admin = require("firebase-admin");
var serviceAccount = require("./key/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://sampleLink.firebaseio.com"
});
const functions = require('firebase-functions');
var db = admin.firestore();
var data = {
name: 'Los Angeles',
state: 'CA',
country: 'USA'
};
// Add a new document in collection "cities" with ID 'LA'
var setDoc = db.collection('cities').doc('LA').set(data);
setDoc
Run Code Online (Sandbox Code Playgroud)
package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase": "^5.7.3",
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.1.0"
},
"private": true
}
Run Code Online (Sandbox Code Playgroud)
控制台返回
tktktk:functions dev$ node index.js
Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail
The behavior for Date objects stored in Firestore is going to change
AND YOUR APP MAY BREAK.
To hide this warning and ensure your app does not break, you need to add the
following code to your app before calling any other Cloud Firestore methods:
const firestore = new Firestore();
const settings = {/* your settings... */ timestampsInSnapshots: true};
firestore.settings(settings);
With this change, timestamps stored in Cloud Firestore will be read back as
Firebase Timestamp objects instead of as system Date objects. So you will also
need to update code expecting a Date to instead expect a Timestamp. For example:
// Old:
const date = snapshot.get('created_at');
// New:
const timestamp = snapshot.get('created_at');
const date = timestamp.toDate();
Please audit all existing usages of Date when you enable the new behavior. In a
future release, the behavior will change to the new behavior, so if you do not
follow these steps, YOUR APP MAY BREAK.
Run Code Online (Sandbox Code Playgroud)
我的环境如下:
$
firebase --version 6.3.0
$ node -v
v8.12.0
$ npm -v
6.4.1
小智 8
我有完全相同的问题。 事实证明,这与节点版本10有关。 我删除了节点10,然后又回到节点8,一切都变得很顺畅...
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
Run Code Online (Sandbox Code Playgroud)
刚打
firebase deploy --only functions
Run Code Online (Sandbox Code Playgroud)
在节点v8中。
在本地运行云函数的正确方法是通过 Functions shell(请参阅https://firebase.google.com/docs/functions/local-emulator)。如果你想像 一样直接运行它$ node index.js,那么你必须自己设置所需的环境变量,否则firebase-functions会抱怨。
但是,请注意,上述消息只是一个警告。尽管如此,您的代码仍在运行。如果您没有看到任何写入 Firestore 的数据,那可能是因为您没有处理 Firestoreset()方法返回的承诺。
小智 5
对我来说,问题是那一行:
const functions = require('firebase-functions');
我刚刚从index.js 中删除了它。我不再使用 Firebase Cloud Function,但我忘记删除该行。这就是我的问题的来源。
所以也许你也把这个包导入到一个不需要它的文件中。
| 归档时间: |
|
| 查看次数: |
8988 次 |
| 最近记录: |