Musawirkhann / node-express-firebase

73 stars 40 forks source link

DB.js > what code do i need? #1

Open MarGer1962 opened 2 years ago

MarGer1962 commented 2 years ago

LS, When i follow you youtube vid and use your code (very usefull good explained by the way:)), i get these errors, when i use Postman. Quote: " App is listening on url http://localhost:8080 [2022-04-13T17:23:18.631Z] @firebase/firestore: Firestore (8.10.1): Connection GRPC stream error. Code: 7 Message: 7 PERMISSION_DENIED: Permission denied on resource project "startapplicatie",. (I named my app: startapplicatie).

In your code (o.a. .env) you use a database-url > this no longer exists in Firestore (at least i can not find it).

I tried the solution to use the Firebase SDK (see: https://firebase.google.com/docs/admin/setup#windows) npm install firebase-admin --save

according to the instructions on this webpage i generated a new private key (Applicationname / Projectsettings / Service Accounts) I saved this in my app directory (same level as config.js) I copied the Node.js code (Admin SDK configuration snippet)

===== code -snippet var admin = require("firebase-admin"); var serviceAccount = require("path/to/serviceAccountKey.json"); admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }); === end code snippet ==

Then i copied the Node.js code into the db.js file like so >

==== code db.js _const firebase = require('firebase'); const config = require('./config');

var admin = require("firebase-admin"); var serviceAccount = require("./startapplicatie-firebase-adminsdk-crck2-2b088c6e08.json"); admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });

// with this contact with the database const db = firebase.initializeApp(config.firebaseConfig); module.exports = db;_

==== code end ==

But when i use npm start > and Postman > i get the same message; Code: 7 Message: 7 PERMISSION_DENIED:

Q: where do i have to put the node.js snippet?

Can you or someone else help me out? Thanks

MarGer1962 commented 2 years ago

SOLVED After some fiddling with the code i came to this conclusion: i had to change the var admin (this 'var admin' is part of the the code snippet that you get if you ask for a new private key: see my question)

== complete code of db.js is now const firebase = require('firebase-admin'); const config = require('./config');

db = require("firebase-admin"); var serviceAccount = require("./startapplicatie-firebase-adminsdk-crck2-2b088c6e08.json"); db.initializeApp({ credential: db.credential.cert(serviceAccount) });

// with this you make contact with firestore database module.exports = db;

== end of to code db.js

Also the problem ("in your code (o.a. .env) you use a database-url > this no longer exists in Firestore (at least i can not find it).") is solved with this solution if you: leave out the reference to the URL - referring to the firestore database - in the config.js and the .env files .

Hope this will help some one :)