Loki00
Loki00

Reputation: 271

bad request error 400 firebase but function works

I'm trying to create a function in my REST API that change a value in a document stored in firebase cloud firestore. I've wrote this function and tested with postman :

index.js

const express = require('express');
const app = express();

app.get('/unsubNewsletter/:userId', newsletterStop);

user.js

exports.newsletterStop = (req,res) =>{
    const document = db.doc(`/users/${req.params.userId}`);
    document
        .get()
        .then(doc =>{
            if(!doc.exists){
                return res.status(404).json({error: 'User not found'})
            } else {
                document.update({newsletter:false})
            }
        })
        .catch(err => {
            console.error(err);
        });
};

The value changed in targeted document but postman shows me this error:

<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 400 (Bad Request)!!1</title>

What can I do to resolve this?

PS if construct that controls if a document exists doesn't check

Upvotes: 0

Views: 1334

Answers (1)

Emil Gi
Emil Gi

Reputation: 1168

According to the documentation you should always end an HTTP function with send(), redirect(), or end(). Otherwise, your function might continue to run and be forcibly terminated by the system.

Add something like:

document.update({newsletter:false})
.then(() => {
  res.status(200).send('Done!');
})

Upvotes: 1

Related Questions