I am trying to retrieve Firestore database information through Express.js used in Firebase cloud functions. My index.js files inside the function folder look like:
const functions = require('firebase-functions');
const admin = require("firebase-admin");
const db = admin.firestore();
admin.initializeApp();
const express = require('express');
const app= express();
app.get("/teacher/:uid",function(req,res){
const id = req.params.uid;
const doc = db.collection('users').doc(id).get();
res.send(doc);
//res.render("teacher",{id : id});
})
the script part of my index.html file looks like:
<body>
// some html code
<!---script follow-->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-functions.js"></script>
<script>
// web app's Firebase configuration
var firebaseConfig = {
apiKey: "xyz",
authDomain: "xyz",
databaseURL: "xyz",
projectId: "xyz",
appId: "xyz"
};
// i have replaced the actual keys and urls with xyz
firebase.initializeApp(firebaseConfig);
const auth= firebase.auth();
const db= firebase.firestore();
const functions = firebase.functions();
</script>
<script src="auth.js"></script>
</body>
ERROR :
on command firebase serve --only functions , i am getting this error
Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
at FirebaseAppError.FirebaseError [as constructor] (/home/chahat/Desktop/firebase/functions/node_modules/firebase-admin/lib/utils/error.js:42:28)
at FirebaseAppError.PrefixedFirebaseError [as constructor] (/home/chahat/Desktop/firebase/functions/node_modules/firebase-admin/lib/utils/error.js:88:28)
at new FirebaseAppError (/home/chahat/Desktop/firebase/functions/node_modules/firebase-admin/lib/utils/error.js:123:28)
at FirebaseNamespaceInternals.app (/home/chahat/Desktop/firebase/functions/node_modules/firebase-admin/lib/firebase-namespace.js:101:19)
at FirebaseNamespace.app (/home/chahat/Desktop/firebase/functions/node_modules/firebase-admin/lib/firebase-namespace.js:452:30)
at FirebaseNamespace.ensureApp (/home/chahat/Desktop/firebase/functions/node_modules/firebase-admin/lib/firebase-namespace.js:468:24)
at Proxy.fn (/home/chahat/Desktop/firebase/functions/node_modules/firebase-admin/lib/firebase-namespace.js:327:30)
at Object.<anonymous> (/home/chahat/Desktop/firebase/functions/index.js:3:18)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
⚠ We were unable to load your functions code. (see above)
auth.js
file as well? – Renaud Tarnec