英文:
How can I update an object without replacing it in Supabase?
问题
我有一个名为updateHistory
的函数:
export const updateHistory = async (id, historyObj) => {
const { data, error } = await supabase
.from("property")
.update({ history: historyObj })
.eq("id", id);
return data;
};
我这样调用它:
useEffect(() => {
let tempHistory = {
column: column,
field: name,
previousValue: `${defaultValue}`,
updatedValue: `${changedValue}`,
time: new Date().toISOString(),
};
let newHistory = [tempHistory, ...history];
setHistory(newHistory);
updateHistory(propertyId, newHistory);
}, [changedValue]);
目标是创建一个记录历史的日志,包括所有先前的值,当前值作为对象数组的第一个元素。问题是,使用这段代码,它只是用当前值替换历史列中的数据,而不是添加并保留先前的值。
我不确定为什么我的当前代码不起作用。
提前感谢。
英文:
I have a function, updateHistory
:
export const updateHistory = async (id, historyObj) => {
const { data, error } = await supabase
.from("property")
.update({ history: historyObj })
.eq("id", id);
return data;
};
I am calling it like so:
useEffect(() => {
let tempHistory = {
column: column,
field: name,
previousValue: `${defaultValue}`,
updatedValue: `${changedValue}`,
time: new Date().toISOString(),
};
let newHistory = [tempHistory, ...history];
setHistory(newHistory);
updateHistory(propertyId, newHistory);
}, [changedValue]);
The goal is to have a log history with all previous values, with the current value being the 1st element in the array of objects. The issue is, with this code, it is just replacing the data in the history column with only the current value, rather than just adding to it and keeping the previous values.
I'm not sure why my current code is not working.
Thanks in advance.
答案1
得分: 0
我已经弄清楚了。这不是Supabase的问题。history
在某些情况下为null,这会将其先前的值替换为空白。
英文:
I figured this out. This is not an issue with Supabase. history
was null at some points, which replaced the previous values of it with nothing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论