Nativescript-plugin-firebase: NativeScript-Vue Firestore Error

Created on 8 Sep 2018  路  4Comments  路  Source: EddyVerbruggen/nativescript-plugin-firebase

Hi @EddyVerbruggen - please assist, I think I followed the docs correctly and trying to implement with NativeScript-Vue.

Firebase auth is working correctly but I get the following terminal error when running firebase.firestore() in the onPanic method in my Vue component below:

Terminating app due to uncaught exception 'NativeScript encountered a fatal error: TypeError: _nativescriptPluginFirebase2.default.firestore is not a function. (In '_nativescriptPluginFirebase2.default.firestore()', '_nativescriptPluginFirebase2.default.firestore' is an instance of Object)

Thanks and much appreciated.

<template>
  <Page actionBarHidden="true">

    <StackLayout class="main-container">

      <Label text="Armour Panic Button" class="app-title" />
      <Button text="Tap in Emergency!" @tap="onPanic" class="armour-button"/>
      <Button text="Logout" @tap="logout" class="default-button logout-link"/>

    </StackLayout>

  </Page>
</template>

<script>
import { firestore } from 'nativescript-plugin-firebase';
import firebase from 'nativescript-plugin-firebase'
const Geolocation = require("nativescript-geolocation");
const Accuracy = require("ui/enums"); 

export default {
  data() {
    return {
      user_id: null,
      lat: null,
      long: null,
      time: null
    }
  },
  methods: {
    onPanic() {
      Geolocation.getCurrentLocation({desiredAccuracy: Accuracy.high, updateDistance: 0.1, timeout: 20000 })
      .then(loc => {
        this.lat = loc.latitude
        this.long = loc.longitude
        this.time = loc.timestamp
        console.log(this.lat, this.long, this.time)
      })

      const usersCollection = firebase.firestore().collection('users')
      const query = usersCollection.where('user_id', '==', this.user_id)

      query.get()
      .then(querySnapshot => {
        querySnapshot.forEach(doc => {
          console.log(doc.data())
        })
      })

    },
    logout() {
      firebase.logout();
    }
  },
  created() {
    firebase.getCurrentUser()
      .then(user => {
        this.user_id = user.uid
        console.log(this.user_id)
      })
      .catch(error => console.log("Trouble in paradise: " + error))
  },
  mounted() {
    Geolocation.isEnabled()
    .then(isEnabled => {
      if(!isEnabled) {
        Geolocation.enableLocationRequest()
      }
    })
    .catch(error => {
      console.log(error.message)
    })
  }
}
</script>

Most helpful comment

use firebase.firestore.collection('users') instead of firebase.firestore().collection('users') and it should work

All 4 comments

use firebase.firestore.collection('users') instead of firebase.firestore().collection('users') and it should work

Thanks for the assistance, @telcy!

Here's an app I'm building using Firestore, Cloud Functions, and the NativeScript-Vue 2.0 template.

Ah @telcy - it worked a charm! Thanks so much and much appreciated. Thanks for the app reference @EddyVerbruggen :)

use firebase.firestore.collection('users') instead of firebase.firestore().collection('users') and it should work

Thanks @telcy - I might be missing it but is this worth including in the docs? I'm quite new to nativescript so perhaps this is my misunderstanding

Was this page helpful?
0 / 5 - 0 ratings