0
votes

I cant find out why what is causing these errors is there any experts out there?

Deployment error. Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs

logs in firebase functions:

Error: function terminated. Recommended action: inspect logs for termination reason. Function cannot be initialized.

code below






var serviceAccount = require("./workservices-e4506-firebase-adminsdk-gawgy-6d4c6a39a1.json");

// admin.initializeApp({
//   credential: admin.credential.cert(serviceAccount),
//   databaseURL: "https://workservices-e4506.firebaseio.com"
// });

admin.initializeApp();

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

const firebase = require('firebase');

  
const firebaseConfig = {
    apiKey: "AIzaSyBEkSLuI3itUsX3iIhvDnnHHT7WoCT76qI",
    authDomain: "workservices-e4506.firebaseapp.com",
    databaseURL: "https://workservices-e4506.firebaseio.com",
    projectId: "workservices-e4506",
    storageBucket: "workservices-e4506.appspot.com",
    messagingSenderId: "232325006209",
    appId: "1:232325006209:web:da0aca044337ef4b119e80",
    measurementId: "G-K8BRW9Y8HL"
  };

//gives error when deploying
firebase.default.initializeApp(firebaseConfig);













app.get('/JobPosts', (req, res) => {
    admin.firestore().collection('JobPosts').orderBy('createdAt','desc').get()
    .then(data => {
        let jobPosts = [];
        data.forEach(doc => {


            // jobPosts.push(doc.data())

            jobPosts.push({
                jobId: doc.id,
                ...doc.data()
            });

        });
    return res.json(jobPosts);
    })
    .catch(err => console.error(err));
    });






app.post('/createJobPost', (req, res) => {

const newJob = {
    jobDescription: req.body.jobDescription,
    jobTitle: req.body.jobTitle,
    createdAt: new Date().toISOString()
};

admin.firestore().collection('JobPosts').add(newJob)
.then(docRef => {
res.json({message: `document ${docRef.id} created succesfully`})
})
.catch(err => {
    //500 server error
    res.status(500).json({error: 'something went wrong'})
    console.error(err)
})
    });





///////////////////////Lesson 5 ////////////////////////////////


//sign up route

app.post('/signup', (req, res) => {

    const user = {
    email: req.body.email,
    password: req.body.password,
    confirmPassword: req.body.confirmPassword,
    username:req.body.username
};


firebase.firestore().doc(`/users/${user.username}`).get()
.then((doc) => {

if(doc.exists){
    ///bad request
    return res.status(400).json({
        username: 'this username is already taken'});
} else {
    return firebase.auth().createUserWithEmailAndPassword(user.email,user.password)
}
})
.then((data) =>{
   return  data.user.getIdToken();

})
.then(token =>{
    // 201 status code indicates that a request was successful and as a result a resource has been created
    return res.status(201).json({ token });
})

.catch((err) => { 
    console.error(err);
    return res.status(500).json({error: err.code });
    })

});
///////////////////////Lesson 5 ////////////////////////////////


  exports.api = functions.region('australia-southeast1').https.onRequest(app);

1

1 Answers

0
votes

If this is the entire contents of your index.js file, you are missing the code to declare a Cloud Function. For more on how to add one, see this minimal example in the documentation.

Aside from that, you are trying to initialize both the Firebase Admin SDK and the client-side SDK in your file, which is both not needed and not supported. In Cloud Functions, you should only be using firebase-admin, so remove everything from const firebase = require('firebase'); and below in the code you posted.