NodeJS 为什么firebase.initializeApp返回FirebaseError:“projectId”未与导入/导出语法一起提供”?

wr98u20j  于 2023-06-22  发布在  Node.js
关注(0)|答案(2)|浏览(140)

我下载了一个修正文件夹为ecomerce项目。这是在控制台'npm start'命令后的错误:x1c 0d1x问题不可能来自firebase对象congig,因为我检查了很多次,我创建了一个新的,我在另一个项目olready中尝试,它工作得很好。我什么都试过了我在网上到处找。我没有找到为我工作的解决方案。我认为问题来自import/import语法。如果有人能试着让这个项目工作,告诉我哪里出了问题,那就太好了:https://github.com/HALLneufmilles/ecom_2-0
下面是我的server.js文件的一部分:

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" });
});
imzjd6km

imzjd6km1#

您正在尝试使用服务帐户初始化Firebase客户端SDK。那是行不通的 * * 仅限客户端SDK**需要使用Firebase控制台提供的配置对象。它看起来像这样:

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

服务帐户用于初始化Firebase Admin SDK,它仅用于后端。它们需要服务帐户才能进行特权操作,这不是您应该在客户端应用中执行的操作。如果您正在编写一个nodejs程序,您可能希望使用Firebase Admin SDK,而不是像现在这样使用客户端SDK。

yks3o0rb

yks3o0rb2#

配置键不正确,应该是projectId vs project_id

相关问题