Search code examples
javascriptnode.jsreactjsfirebasefirebase-cli

Why firebase serve is not working but firebase deploy is working fine?


whenever I use firebase deploy it creates data successfully but whenever I use firebase serve then it goes to catch function! The function i am using is:

exports.createUser = functions.https.onRequest((req, res) => {

    const newUser = {
        body: req.body.body,
        userName: req.body.userName,
        createdAt: admin.firestore.Timestamp.fromDate(new Date())
    };
    admin
        .firestore().collection('screems').add(newUser)
        .then(doc => {
            return res.json({
                message: `document ${doc.id} created successfully`
            });
        })
        .catch(err => {
            res.status(500).json({
                error: 'something went wrong'
            });
            console.error(err);
        });
});

I use postman to check the API but it always says "something went wrong";


Solution

  • As stated in the documentation about running functions locally.

    If you have Cloud Functions that use the Firebase Admin SDK to write to Cloud Firestore, these writes will be sent to the Cloud Firestore emulator if it is running. If further Cloud Functions are triggered by those writes, they will be run in the Cloud Functions emulator.

    The deployed function connects to your actual Cloud Firestore instance, but when you run your function locally, you need to install, configure, and integrate the emulators your Cloud Function is supposed to use.