如何在Firestore中修复“无法读取null的属性'firestore'”

时间:2019-06-03 14:20:00

标签: firebase google-cloud-firestore nuxt.js

我将尝试在firestore()函数中更改设置,但不起作用。 我阅读了这份文件,他们说这个将来将在将来的版本中删除,但是我没有看到解决方法。所以我希望任何人都可以帮助我解决这个问题

操作系统版本:Majove 10.14.2 库版本:^ 6.1.0 Firebase产品:firestore

import * as firebase from 'firebase/app'
import 'firebase/firestore'
require('dotenv').config({ encoding: 'utf8' })

const firebaseConfig = {
  apiKey: process.env.API_KEY,
  authDomain: process.env.AUTH_DOMAIN,
  databaseURL: process.env.DATABASE_URL,
  projectId: process.env.PROJECT_ID,
  storageBucket: process.env.STORAGE_BUCKET,
  messagingSenderId: process.env.MESSAGING_SENDER_ID,
  appId: process.env.APP_ID
}

// Initialize Firebase
let firebaseApp = null
if (!firebase.app.length) {
  firebaseApp = firebase.initializeApp(firebaseConfig)
}
firebaseApp.firestore().settings({
  ssl: false,
  timestampsInSnapshots: true
})

export default firebaseApp.firestore()

1 个答案:

答案 0 :(得分:0)

请参见https://firebase.google.com/docs/web/setup

初始化firebase的基本方法是这样。

import firebase from "firebase/app";
import "firebase/auth";
import "firebase/firestore";
import "firebase/functions";
import "firebase/storage";
import "firebase/messaging";
import "firebase/performance";

const config = {
  // set your config here
};

if (!firebase.apps.length) {
  firebase.initializeApp(config);
}

const auth = firebase.auth();
const storage = firebase.storage();
const functions = firebase.functions();
const firestore = firebase.firestore();
let messaging = null;
try {
  if (firebase.messaging.isSupported()) {
    messaging = firebase.messaging();
    messaging.usePublicVapidKey("your publicVapidKey here");
  }
} catch (e) {}
const perf = firebase.performance();

export { firebase, auth, storage, functions, firestore, messaging };

我编辑了您的代码。

import firebase from 'firebase/app'
import 'firebase/firestore'
require('dotenv').config({ encoding: 'utf8' })

const firebaseConfig = {
  apiKey: process.env.API_KEY,
  authDomain: process.env.AUTH_DOMAIN,
  databaseURL: process.env.DATABASE_URL,
  projectId: process.env.PROJECT_ID,
  storageBucket: process.env.STORAGE_BUCKET,
  messagingSenderId: process.env.MESSAGING_SENDER_ID,
  appId: process.env.APP_ID
}

if (!firebase.apps.length) {
  firebase.initializeApp(firebaseConfig)
}
const firestore = firebase.firestore();
firestore.settings({
  ssl: false,
  timestampsInSnapshots: true
})

export default firestore;