Get a collection from Firestore in Unity?

Viewed 1951

I am trying to get a firestore collection in Unity by following this link, but it gives me an error:

'Task' does not contain a definition for 'Documents' and no accessible extension method 'Documents' accepting a first argument of type 'Task'.

My Code:

CollectionReference allCitiesQuery = db.Collection("MyList");
Task<QuerySnapshot> allCitiesQuerySnapshot = allCitiesQuery.GetSnapshotAsync();

foreach (DocumentSnapshot documentSnapshot in allCitiesQuerySnapshot.Documents)
      {           
          Dictionary<string, object> city = documentSnapshot.ToDictionary();
          foreach (KeyValuePair<string, object> pair in city)
          {
            Debug.Log(pair.Key + " " + pair.Value);
            //Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
          }
      }
    }

I have read the documentation but there is very little focus on Unity because a few days ago it was just implemented. Can anyone help me here, please?

2 Answers

Try changing

CollectionReference allCitiesQuery = db.Collection("MyList");
Task<QuerySnapshot> allCitiesQuerySnapshot = allCitiesQuery.GetSnapshotAsync();

into

Query allCitiesQuery = db.Collection("MyList");
QuerySnapshot allCitiesQuerySnapshot = await allCitiesQuery.GetSnapshotAsync();

and ensure you have all the following included:

using Firebase;
using Firebase.Firestore;
using Firebase.Extensions;

Here is the functional code for those who have a related problem.

using Firebase;
using Firebase.Extensions;
using Firebase.Firestore;
using System.Threading;
using System.Threading.Tasks;

public class Example  : MonoBehaviour
{

public  async void GetMyCollections()
    {
      Query allCitiesQuery = db.Collection("MyRootCollection");
      QuerySnapshot allCitiesQuerySnapshot = await allCitiesQuery.GetSnapshotAsync();

      foreach (DocumentSnapshot documentSnapshot in allCitiesQuerySnapshot.Documents)
      {
          Dictionary<string, object> city = documentSnapshot.ToDictionary();
          foreach (KeyValuePair<string, object> pair in city)
          {
            Debug.Log(pair.Key + " " + pair.Value);
          }
      }
    }
}

References: FirebaseDocs, Async programming.

Related