[英]How to continuously add objects to nested collection in Firestore
I am writing a React/Redux app that uses Firebase Auth/Firestore to keep track of a user's gym exercises.我正在编写一个 React/Redux 应用程序,它使用 Firebase Auth/Firestore 来跟踪用户的健身锻炼。 I have Redux Form to handle data submission and I have the below example data structure I want to achieve in Firestore:我有 Redux 表单来处理数据提交,我有下面的示例数据结构,我想在 Firestore 中实现:
users {
id {
name: 'John Smith'
uid: 'k1s7fxo9oe2ls9u' (comes from Firebase Auth)
exercises: {
{
name: 'Bench Press',
sets: 3,
reps: 10,
lbs: 100,
}
}
}
}
However, I can't figure out how to keep adding new exercise objects to the exercises subcollection (in Firestore I guess it would be a field type of map).但是,我不知道如何继续将新的练习对象添加到练习子集合中(在 Firestore 中,我猜这将是地图的字段类型)。 What I want to do is have new objects in "exercises" as the user submits new forms.当用户提交新的 forms 时,我想要做的是在“练习”中添加新对象。 So for example, if the user wanted to add a Deadlift exercise, it would look like the below:例如,如果用户想添加一个硬拉练习,它看起来像下面这样:
users {
id {
name: 'John Smith'
uid: 'k1s7fxo9oe2ls9u' (comes from Firebase Auth)
exercises: {
{
name: 'Bench Press',
sets: 3,
reps: 10,
lbs: 100,
},
{
name: 'Deadlift',
sets: 3,
reps: 12,
lbs: 120,
}
}
}
}
Calling db.collection('users').doc(doc.id).add({"exercises": values});
调用db.collection('users').doc(doc.id).add({"exercises": values});
just updates the Bench Press object that's there already rather than adding a new one on submission of the form.只需更新已经存在的台式压力机 object,而不是在提交表单时添加新的。
But calling db.collection('users').doc(doc.id).add({"exercises": values});
但是调用db.collection('users').doc(doc.id).add({"exercises": values});
gives me this error: Uncaught (in promise) TypeError: _firebase__WEBPACK_IMPORTED_MODULE_3__.default.collection(...).doc(...).add is not a function.给我这个错误: Uncaught (in promise) TypeError: _firebase__WEBPACK_IMPORTED_MODULE_3__.default.collection(...).doc(...).add is not a function。
I've been struggling with this for quite a while, any help is hugely appreciated.我已经为此苦苦挣扎了一段时间,非常感谢任何帮助。
This is my component:这是我的组件:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Field, reduxForm } from 'redux-form';
import db from '../../../firebase';
import '@firebase/firestore';
import { store } from '../../../App';
const formSubmit = (values)=> {
const currentUserId = store.getState().auth.uid;
db.collection("users").get().then((usersSnapshot) => {
usersSnapshot.forEach((doc) => {
// looking for the current user and then updating their data
if(doc.data().uid === currentUserId) {
db.collection('users').doc(doc.id).add({
"exercises": values,
});
}
});
});
}
let ExercisesForm = ({ handleSubmit }) => (
<form onSubmit={handleSubmit(formSubmit)}>
<div>
<Field name="name" component="input" type="text" placeholder="Exercise Name" />
</div>
<div>
<Field name="sets" component="input" type="number" />
<label htmlFor="sets"> sets</label>
</div>
<div>
<Field name="reps" component="input" type="number" />
<label htmlFor="reps"> reps</label>
</div>
<div>
<Field name="lbs" component="input" type="number" />
<label htmlFor="lbs"> lbs</label>
</div>
<button type="submit">Submit</button>
</form>
)
ExercisesForm = reduxForm({
form: 'exercise'
})(ExercisesForm)
const mapStateToProps = state => ({
uid: state.auth.uid,
});
export default connect(
mapStateToProps,
undefined
)(ExercisesForm);
You should be able to say: 您应该能够说:
db
.collection('users')
.doc(doc.id)
.collection('exercises')
.add(values);
Where values
contains all the fields of the document you want to add. 其中values
包含要添加的文档的所有字段。 It will create a new document with a random id in the exercises subcollection. 它将在Exercises子集合中创建一个具有随机ID的新文档。
So this won't work in the latest version of Firebase V9 .所以这在最新版本的Firebase V9中不起作用。
So below is how I added a subcollection to a document in the latest version:下面是我如何在最新版本的文档中添加子集合:
const docRef = doc(database, "userDocs", session.user.email);
const colRef = collection(docRef, "docs");
await addDoc(colRef, {
fileName: input,
timestamp: serverTimestamp(),
})
.then(() => {
console.log("CREATED");
})
.catch((err) => {
console.error("Error creating document", err);
});
This will created a structure like这将创建一个结构,如
userDocs
-> userEmail
-> docs
-> uid
-> data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.