Constantin Beer
Constantin Beer

Reputation: 5835

How to solve "TypeError: Cannot read property 'firstname' of undefined" in Firebase with Cloud Functions?

I'm trying to create a Document in my Firestore db by using Cloud Functions. Also using Postman to send the folowing POST:

{
"firstname": "TestFirst2",
"lastname ": "TestLast2",
"email": "[email protected]"`enter code here`
}

This is the Function I'm trying to execute:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.addUserReq = functions.https.onRequest((request, response) => {
   const firstname = String(request.data.firstname)

   const col = admin.firestore().collection(`users`)
   col.add({
       firstname: firstname,
       lastname: request.data.lastname,
       email: request.data.email})
       .then(snapshot => {
       const data = snapshot.data()
       return response.send(data);
    })
    .catch(error => {
       console.log(error)
       response.status(500).send(error)
    })
})

And this is the ErrorMessage:

TypeError: Cannot read property 'firstname' of undefined
at exports.addUserReq.functions.https.onRequest (/user_code/index.js:14:34)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /var/tmp/worker/worker.js:735:7
at /var/tmp/worker/worker.js:718:11
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)

I already searched in the Firebase docs, but didn't found something. How can I define the param "firstname" to avoid the errorMessage?

Update: This is a snippet of the index.js of my web app that is causing the same results as the request with Postman.

function save() {

 var userFirstname = document.getElementById("firstname_field").value;
 var userLastname = document.getElementById("lastname_field").value;
 var userEmail = firebase.auth().currentUser.email_id;

 var addUser = functions.httpsCallable('addUserReq');
 addUser({
   "users": {
       "firstname": userFirstname,
       "lastname": userLastname,
       "email": userEmail
     }
   }
   ).then(function(result) {
   var result = result;
 }).catch((err) => {
   alert(err)
 });
}

Upvotes: 1

Views: 628

Answers (2)

Peter Haddad
Peter Haddad

Reputation: 80924

Change this:

exports.addUserReq = functions.https.onRequest((request, response) => {
const firstname = String(request.data.firstname)

into this:

 exports.addUserReq = functions.https.onRequest((request, response) => {
const firstname = String(request.body.firstname)

Firebase uses the express framework to be able to do http requests.

According to the docs:

Used as arguments for onRequest(), the Request object gives you access to the properties of the HTTP request sent by the client, and the Response object gives you a way to send a response back to the client.

The request object of the express framework has the property body that contains the data submitted: http://expressjs.com/en/4x/api.html#req.body

Upvotes: 2

Vignesh
Vignesh

Reputation: 1222

     col.add({
      // firstname: firstname, ==> where is this firstname comming from?,
firstname:request.data.firstname,
lastname: request.data.lastname,
       email: request.data.email})
       .then(snapshot => {
       const data = snapshot.data()
       return response.send(data);
    })
    .catch(error => {
       console.log(error)
       response.status(500).send(error)
    })

Upvotes: 0

Related Questions