skip to Main Content

I dowloaded a correction folder for ecomerce project.
This is the error in console after the ‘npm start’ command:enter image description here
The problem can’t come from the firebase object congig because I checked many time, I create a new one that I tried in another project olready in line and it works well.
I tried everything. I looked for everywhere in internet. I didn’t find the solution who works for me.
I think the problem come from import/import syntaxe.
if anyone could try to make this project work and tell me what’s wrong that would be great:
https://github.com/HALLneufmilles/ecom_2-0

here is a part of my server.js file:

import express from "express";
import bcrypt from "bcrypt";
import { initializeApp } from "firebase/app";
import { getFirestore, doc, collection, setDoc, getDoc, updateDoc, getDocs, query, where, deleteDoc, limit } from "firebase/firestore";
import stripe from "stripe";

// Your web app's Firebase configuration
const firebaseConfig = {
  type: "service_account",
  project_id: "ecom-website-ef3e5",
  private_key_id: "fcb20266224adb06a42e19754aa76029863492c4",
  private_key: "........................",
  client_email: "................gserviceaccount.com",
  client_id: "1..................0",
  auth_uri: "https....................th2/auth",
  token_uri: "https://o.......................oken",
  auth_provider_x509_cert_url: "htt.....................rviceaccount.com",
  universe_domain: "go.......com",
};
// Initialize Firebase
const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);

// init server
const app = express();

// middlewares
app.use(express.static("public"));
app.use(express.json()); // enables form sharing

// aws
import aws from "aws-sdk";
import "dotenv/config";

// aws setup
const region = "ap-south-1";
const bucketName = "ecom-website-4";
const accessKeyId = process.env.AWS_ACCESS_KEY;
const secretAccessKey = process.env.AWS_SECRET_KEY;

aws.config.update({
  region,
  accessKeyId,
  secretAccessKey,
});

// init s3
const s3 = new aws.S3();

// generate image url
async function generateURL() {
  let date = new Date();

  const imageName = `${date.getTime()}.jpeg`;

  const params = {
    Bucket: bucketName,
    Key: imageName,
    Expires: 300, // 300 ms
    ContentType: "image/jpeg",
  };

  const uploadURL = await s3.getSignedUrlPromise("putObject", params);
  return uploadURL;
}

app.get("/s3url", (req, res) => {
  generateURL().then((url) => res.json(url));
});

// routes
// home route
app.get("/", (req, res) => {
  res.sendFile("index.html", { root: "public" });
});

// signup
app.get("/signup", (req, res) => {
  res.sendFile("signup.html", { root: "public" });
});

2

Answers


  1. The config keys are incorrect, it should be projectId vs project_id

    Login or Signup to reply.
  2. You’re trying to use a service account to initialize the Firebase client SDK. That won’t ever work. You need to use the configuration object provided by the Firebase console for the client SDK only. It looks like this:

      {
        apiKey: "<API_KEY>",
        authDomain: "<PROJECT_ID>.firebaseapp.com",
        databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
        projectId: "<PROJECT_ID>",
        storageBucket: "<BUCKET>.appspot.com",
        messagingSenderId: "<SENDER_ID>",
      };
    

    Service accounts are used to initialize the Firebase Admin SDK, which is for backends only. They require the service account for privileged operations, which are not things you should ever be doing in your client app. If you are writing a nodejs program, you probably want to use the Firebase Admin SDK instead of the client SDK as you are now.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search