Firestore.settings() requires its first argument to be of type object but it was a custom object

Viewed 1812

I can't call the firebase.firestore().settings() function as I'm getting some "requires its first argument to be of type object" error:

firebase.firestore().settings({ timestampsInSnapshots: true })

I don't understand this error, but as a workaround I tried to create an object thus:

const firestoreSettings = Object.create({ timestampsInSnapshots: true })
firebase.firestore().settings(firestoreSettings)

This didn't work either.

Where does this error come from and how to solve it?

3 Answers

you are confusing the namespaces; this should do the trick

import firebase from 'firebase/app'
if (!firebase.apps.length) {
   firebase.initializeApp(YOUR_CONFIG_OBJECT_GOES_HERE)
}
const firestore = firebase.firestore()
const settings = { timestampsInSnapshots: true }
firestore.settings(settings) // notice it is firestore instead of firestore()

PD: This is not required anymore... try updating your sdk version to the last one :)

The following should do the trick:

import firebase from 'firebase/app';
import 'firebase/firestore';

const config = {
    apiKey: 'xxxxxxxxx',
    authDomain: 'xxxxxxxxx',
    ......
};

firebase.initializeApp(config);

const db = firebase.firestore();

const settings = {
    timestampsInSnapshots: true
};
db.settings(settings);

//....

As indicated in the other answer, note that you don't need use the timestampsInSnapshots setting anymore, see https://firebase.google.com/docs/reference/js/firebase.firestore.Settings#optional-timestamps-insnapshots

I noticed that this issue was tagged with nuxt.js. I came across this issue recently when initializing firestore with settings in a Nuxt app. I solved the issue by making sure that firestore().settings() was only called client side - not server side.

Related