【发布时间】:2019-05-22 15:37:26
【问题描述】:
我的事件集合中的文档的自动生成 ID 可能出错了。我为生成的事件添加了 eventId,并为每个事件手动分配了 eventId。
我能否以某种方式获取带有 eventId 的最后一个文档,并添加新文档,最后一个文档的 eventId 加一。
或者我应该删除自动生成的基于 Id 的事件并使用非自动生成的 id 创建新的事件?
GitHub:https://github.com/Verthon/event-app
我已经在 netlify 上发布了正在运行的 React.js 应用程序:https://eventooo.netlify.com/
它是如何工作的:
- 我为事件集合内的每个虚拟事件添加了唯一的 eventId,
- 基于这个唯一的 eventId,我创建了指向特定单个 Event.js 的链接,
- 用户可以在 /create-event 中创建提供信息的事件,
- 一旦有人创建了一个事件,我想将事件添加到带有递增 id 的事件集合中,我已经添加了 7 个在控制台内创建的事件,所以接下来应该有 id=7,可能类似于 event1、event2 ...自动递增
- 在用户集合中,我存储来自用户提供的身份验证和主机名的 currentUser.uid
Event Creator
submitEvent = (e) => {
e.preventDefault();
const eventRef = this.props.firebase.db.collection("events").doc();
eventRef.set({
title: this.state.title,
host: this.state.host,
localization: this.state.localization,
description: this.state.description,
category: this.state.category,
day: this.state.day,
hour: this.state.hour,
featuredImage: this.state.imageUrl,
uid: this.props.firebase.auth.currentUser.uid
});
const citiesRef = this.props.firebase.db.collection("cities").where("city", "==", this.state.localization);
const cityRef = this.props.firebase.db.collection("cities").doc();
citiesRef.get()
.then(querySnapshot => {
if(querySnapshot.exists){
console.log("exist");
return
}else{
cityRef.set({
city: this.state.localization,
})
}
});
const userRef = this.props.firebase.db.collection("users").doc();
userRef.set({
user: this.state.host,
uid: this.props.firebase.auth.currentUser.uid
});
谢谢
【问题讨论】:
标签: firebase google-cloud-firestore