【发布时间】:2021-10-18 17:48:07
【问题描述】:
如何将我刚刚编辑的值更新到 firebase? 到目前为止,这是我从 editRow 了解到的(如果我错了,请纠正我)。
DataGrid 内部
//Enable edit mode for row and not cell
editMode="row"
//any update you do in the row is stored in this variable so when you finish it overrides
//w/e you had there with editRowsModel value
editRowsModel={editRowsModel}
//Calls for the function that will be handeling the updates and the all the updates into "model"
onEditRowsModelChange={handleEditRowsModelChange}
//For Double Click + Control
//(That way if uses double click by mistake it doesn't enable editMode)
onCellDoubleClick={(params, event) => {
if (!event.ctrlKey) {
event.defaultMuiPrevented = true;
}
}}
常量
//Variable that will hold the edited variable
const [editRowsModel, setEditRowsModel] = React.useState({});
//Edit function
const handleEditRowsModelChange = React.useCallback((model) => {
setEditRowsModel(model);
}, []);
现在我想在编辑功能中编辑该行,但如何更新到 Firebase? 就像我有这个:
const [editRowsModel, setEditRowsModel] = React.useState({});
const handleEditRowsModelChange = React.useCallback((model) => {
setEditRowsModel(model);
console.log(model.uid)
/*
estudiantes.filter( x => {
const temporalQuery = db.collection("usuarios").doc(user.uid).collection("estudiantes").doc(x.uid);
temporalQuery.update({
nombre: model.nombre,
colegio: model.colegio,
grado: model.grado
})
})
*/
}, []);
但是这不起作用,因为我看不到模型中的各个值,因为它只是说“未定义”,否则这将起作用。如何获得模型的各个值?
因为它是未定义的,所以当我尝试该代码时会出错。
欢迎任何帮助/提示。
模型如何记录
console.log(model)
console.log(model.name)
console.log(model[0].name)
【问题讨论】:
-
所以您想检测用户何时完成编辑并在 firebase 上更新提交的数据,对吗?
-
如果我只是 console.log 的“模型”,我可以随时看到值,模型显示整行数据,但是如果有意义的话,我知道如何单独获取这些数据?因为我不能只是简单地做 model[0].name 或类似的事情,就像我在它出现未定义之前所做的那样让我上传它是如何打印的
-
individually take this data,你是指用户输入或提交新行数据的时候? -
当我进入编辑模式时,模型会自动存储行数据,我想用这些数据来更新 firebase,但是当我尝试记录“数组的一部分”时它显示为未定义。 idk,如果我正确地解释我自己
-
编辑模型不是数组,它是一个对象,键是行id,值是编辑后的行数据。
标签: reactjs firebase google-cloud-firestore datagrid material-ui