【发布时间】:2020-12-28 22:07:31
【问题描述】:
该项目是使用 Vercel 部署的 - 作为该过程的一部分,我在 Vercel 仪表板上添加了我的 firebase 安全/项目详细信息作为环境变量。
Auth 在部署时可以正常工作,但这是控制台中的错误:
@firebase/firestore:Firestore (7.17.2):无法访问云 Firestore 后端。连接失败 1 次。最近的错误: FirebaseError: [code=invalid-argument]: 项目 id "my-project-id" 是 格式错误:它包含无效字符或太长。看 在 https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects 有关如何获取项目 ID 的说明。这通常 表示您的设备没有健康的互联网连接 眼下。客户端将在离线模式下运行,直到 能够成功连接到后端。
我在 package.json 中的依赖:
"dependencies": {
"@material-ui/core": "^4.11.0",
"@material-ui/icons": "^4.9.1",
"firebase": "^7.15.5",
"firebase-admin": "^8.12.1",
"js-cookie": "2.2.1",
"next": "latest",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-firebaseui": "4.1.0",
"swr": "0.2.3"
我不确定我的代码的哪些部分最有用。下面是当前在本地工作但在部署时不工作的代码 sn-ps。
(处理表单提交)
const handleSubmit = (e) => {
e.preventDefault();
if (validate()) var user = firebase.auth().currentUser;
var db = firebase.firestore();
db.collection("users")
.doc(user.uid)
.collection("clients")
.doc()
.set({
name: values.fullName,
email: values.email,
tel: values.mobile,
postcode: values.postcode,
})
.then(function () {
console.log("Document Succesfully Written");
})
.catch(function (error) {
console.error("Error writing document: ", error);
});
};
(查看客户端文档列表)
const Clients = () => {
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
const db = firebase.firestore();
db.collection("users")
.doc(user.uid)
.collection("clients")
.onSnapshot((snap) => {
const clients = snap.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));
setClients(clients);
});
} else {
[];
}
});
const [clients, setClients] = useState([]);
return (
<div>
<ul>
{clients.map((client) => (
<li key={client.id}>{client.name}</li>
))}
</ul>
</div>
);
};
export default Clients;
【问题讨论】:
标签: javascript firebase google-cloud-firestore next.js vercel