Ad

Can't Post To Firebase's Database

When I try to post data with postman to firebase I get this error: Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field body).

I checked and body and userHandle comes back as undefined but I don't know why. With postman I use POST and in the body I send a json like this:

{
    "body": "scream 2",
    "userHandle": "user2"
}

as raw JSON(application/json)

const functions = require('firebase-functions');

const admin = require('firebase-admin');

let firebaseConfig = {
  apiKey: '*******',
  authDomain: '******',
  databaseURL: '*****',
  projectId: '*****',
  storageBucket: '****',
  messagingSenderId: '*****',
  appId: '*******'
};

admin.initializeApp(firebaseConfig);


exports.createScream = functions.https.onRequest((req, res) => {
  if (req.method !== 'POST') {
    return res.status(400).json({ error: 'Method not allowed' });
  }
  const newScream = {
    body: req.body.body,
    userHandle: req.body.userHandle,
    createdAt: admin.firestore.Timestamp.fromDate(new Date())
  };

  admin
    .firestore()
    .collection('screams')
    .add(newScream)
    .then(doc => {
      res.json({ message: `document ${doc.id} created successfully` });
    })
    .catch(err => {
      res.status(500).json({ error: 'something went wrong' });
      console.error(err);
    });
});

Ad

Answer

Okay it's a little weird but in postman the body was set to raw JSON (application/json)but I set it again to JSON(application/json) and now it's working.

Ad
source: stackoverflow.com
Ad