ReallySmallSoftware / cordova-plugin-firestore

A Google Firebase Firestore plugin to enable realtime synchronisation between app and cloud and automatically handle limited connectivity.
Other
22 stars 10 forks source link

How to use the collection function, are there any example apps? #5

Open cyrusonline opened 5 years ago

cyrusonline commented 5 years ago

Expected Behavior

Actual Behavior

Steps to Reproduce the Problem

Specifications

wf9a5m75 commented 5 years ago
document.addEventListener('deviceready', function() {
  var options = {
    "datePrefix": '__DATE:',
    "fieldValueDelete": "__DELETE",
    "fieldValueServerTimestamp" : "__SERVERTIMESTAMP",
    "persist": true
  };

  if (cordova.platformId === "browser") {

    options.config = {
      apiKey: "(your api key)",
      authDomain: "localhost",
      projectId: "(your project id)"
    };
  }

  Firestore.initialise(options).then(function(db) {
    // Add a second document with a generated ID.
    db.collection("users").add({
        first: "Alan",
        middle: "Mathison",
        last: "Turing",
        born: 1912
    })
    .then(function(docRef) {
        console.log("Document written with ID: ", docRef.id);
    })
    .catch(function(error) {
        console.error("Error adding document: ", error);
    });
  });
});
ukuleleplayer commented 5 years ago

When I'm trying to replicate this Firestore initialization, I get the following:

Uncaught Error: Cannot instantiate firebase-firestore - be sure to load firebase-app.js first.
    at index.esm.js:17501
    at index.esm.js:41

I thought it was taken care of in the plugin, so I'm probably doing something wrong?

wf9a5m75 commented 5 years ago

Please share your project files in order to reproduce your issue.

ukuleleplayer commented 5 years ago

Which files should I share? In package.json cordova-plugin-firestore is listed as dependency and cordova plugin. in config.xml cordova-plugin-firestore is listed as plugin.

wf9a5m75 commented 5 years ago

In order to reproduce your issue, please all files (www, config.xml, package.json at least) on to GitHub. Create a repository, then put them to there please.

What will do are:

$> git clone (repo)

$> cd (repo)

$> npm i

$> cordova prepare

$> cordova run browser/android/ios

You don't need to share actual your project code, but please create a project that reproduce your issue 100% instead. Thank you for your understanding and cooperation.

ukuleleplayer commented 5 years ago

Thanks for your quick responses! Here is a vanilla repo I just made - it has the same issue: https://github.com/ukuleleplayer/firestore_plugin

wf9a5m75 commented 5 years ago

Thank you for sharing the project. The problem is you use older version(1.3.0). https://github.com/ukuleleplayer/firestore_plugin/blob/179e650/config.xml#L26

The latest version is 1.3.2, which is only available from Github. The following steps solves this problem.

$> cordova plugin rm cordova-plugin-firestore

$> cordova plugin add https://github.com/ReallySmallSoftware/cordova-plugin-firestore

I don't know why @ReallySmallSoftware does not release the version. Maybe he has some reasons.


You will notice another error.

screen shot 2018-10-18 at 5 53 07 pm

This error occurs because timestampsInSnapshots is false.

I created timestampsInSnapshots is true by default, by the author of this plugin (@ReallySmallSoftware ) changes the default settings to keep the backward compatibility.

https://github.com/ReallySmallSoftware/cordova-plugin-firestore/commit/6c1429a9bd551062db656a1819aa8f409eb7e465

screen shot 2018-10-18 at 5 54 27 pm

To prevent this error message, you need to set timestampsInSnapshots option.


            // Initialize Firebase
            var options = {
                "datePrefix": '__DATE:',
                "fieldValueDelete": "__DELETE",
                "fieldValueServerTimestamp": "__SERVERTIMESTAMP",
                "persist": true,
                "timestampsInSnapshots": true,  // <-- set true
                "config": {}
            };
ReallySmallSoftware commented 5 years ago

I will be releasing a new version shortly - I am testing the recent changes and prefer to integrate and test with a real world app that uses a lot of the functionality to make sure there are no obvious breaking changes.

Sorry if this takes a little longer than is ideal, but this is only a side project!

ReallySmallSoftware commented 5 years ago

An update: My testing has shown a significant number of breaking changes with the latest version that is in github. I am working to correct these and finish support for Geopoint and Timestamp.