Revolutionary Citron
Revolutionary Citron

Reputation: 115

Unexpected error when querying for a field from Firebase

I am using React.js to query a field in a document from Firebase. However, when I try to get the data, I keep getting an error. The code, the collection on Firestore, and and the error message are below. How do I properly query specific fields of data without running into this error?

import React from 'react';
import styled from 'styled-components';
import { firestore } from '../../firebase/firebase.utils';

class Economics extends React.Component {
  constructor() {
    super();

    this.state = {
      name: firestore.collection('blog-categories').doc('8uVaHd22tT5oXSzpOOuj').get('name') 
    }
  }


  render() {
    return (
      <div>
        hi
      </div>
    )
  }
}

export default Economics;

enter image description here enter image description here

Upvotes: 0

Views: 284

Answers (1)

vitooh
vitooh

Reputation: 4272

I think this is proper reference for the API you are using.

When you enter parameter to get method API is expecting options object, so when you enter String "name" there you will get this error massage.

The method is asynchronous so you have to take value form DocumentSnapshot (reference) object returned as Promise which result of the method.

I hope it will help!

UPDATE: As requested I'm adding example:

Firestore has collection collection1 with document doc1 that has field field1 with value 'value1':

var Firestore = require('@google-cloud/firestore');
var db = new Firestore();

var docRef = db.collection("collection1").doc("doc1");

docRef.get().then(docSnap => {
    console.log(docSnap.data().field1);
    console.log(docSnap.get("field1"));
});

If you run this in node result is:

value1
value1

Upvotes: 1

Related Questions