我的 firebase.js 中有这个
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getStorage } from "firebase/storage";
import { getFirestore } from "firebase/firestore";
const firebaseConfig = {
apiKey: "mykey",
authDomain: "lorem",
projectId: "ipsum",
storageBucket: "dolor",
messagingSenderId: "somit",
appId: "lorem",
};
// Initialize Firebase
export const app = initializeApp(firebaseConfig);
export const auth = getAuth();
export const storage = getStorage();
export const db = getFirestore();
它应该是正确的,但是当我尝试连接到服务器以上传有关用户的信息时,我得到了这个:
我对 React 很陌生,我正在学习如何使用 React 和 firebase 制作聊天应用程序的教程。在控制台中,它只是说 POST 请求返回了状态代码为 400 的 404。
正如您在下面看到的那样,它在第一次连接时确实有效,但当我提交时却没有。
这是试图发布信息的代码
import React, { useState } from "react";
import Add from "../img/addImage.png";
import { createUserWithEmailAndPassword, updateProfile } from "firebase/auth";
import { auth, storage, db } from "../firebase";
import { ref, uploadBytesResumable, getDownloadURL } from "firebase/storage";
import { doc, setDoc } from "firebase/firestore";
const Register = () => {
const [err, setErr] = useState(false);
const handleSubmit = async (e) => {
e.preventDefault();
const displayName = e.target[0].value;
const email = e.target[1].value;
const password = e.target[2].value;
const file = e.target[3].files[0];
console.log(displayName + " " + email + " " + password);
try {
const res = await createUserWithEmailAndPassword(auth, email, password);
const storageRef = ref(storage, displayName);
await uploadBytesResumable(storageRef, file).then(() => {
getDownloadURL(storageRef).then(async (downloadURL) => {
try {
//Update profile
await updateProfile(res.user, {
displayName,
photoURL: downloadURL,
});
//create user on firestore
await setDoc(doc(db, "users", res.user.uid), {
uid: res.user.uid,
displayName,
email,
photoURL: downloadURL,
});
} catch (err) {
console.log(err);
setErr(true);
}
});
});
} catch (err) {
setErr(true);
}
};
return (
<div className="formContainer">
<div className="formWrapper">
<span className="logo">Elena Chat</span>
<span className="title">Register</span>
<form onSubmit={handleSubmit}>
<input type="text" placeholder="Display Name" />
<input type="email" placeholder="Email" />
<input type="password" placeholder="Password" />
<input style={{ display: "none" }} type="file" id="file" />
<label htmlFor="file">
<img src={Add} alt="" />
<span>Add Profile Picture</span>
</label>
<button>Sign Up</button>
{err && <span>Something went wrong</span>}
</form>
<p>Already have an account? Login</p>
</div>
</div>
);
};
export default Register;
我在 firebase 上开始了一个新项目来获得一个新的 api 密钥,我得到了完全相同的错误。
我也试着把事情评论出来,只是尝试不同的事情,我得到了同样的错误。