如何在Supabase中更新对象而不替换它?

huangapple go评论57阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月10日 14:31:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定