You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't know if this is expected behavior or not. But it feels off for me.
It will cause issues when using defaultData multiple times.
(ex. multiple lowdb instances using the same defaultData)
ISSUES:
defaultData is used as a reference for data in lowdb object.
when data in lowdb is updated the defaultData object content is also updated.
please check the example typescript code below.
import{JSONFilePreset}from"lowdb/node"constdefaultData={name: "ALICE",}constdb=awaitJSONFilePreset("test.json",defaultData)// db.data should be ALICEconsole.log(db.data.name==="ALICE")// true// update db.data to BOBdb.data.name="BOB"// db.data now is BOBconsole.log(db.data.name==="BOB")// true// defaultData should still be ALICEconsole.log(defaultData.name==="ALICE")// false// defaultData becomes BOB instead (unexpected)console.log(defaultData.name==="BOB")// true
Пояснення:
Проблема виникає через те, що defaultData передається за посиланням в lowdb. Зміни в db.data впливають на оригінальний об’єкт defaultData. Щоб уникнути цього, створіть глибоку копію defaultData за допомогою JSON.parse(JSON.stringify(defaultData)).
Такий підхід гарантує, що зміни в db.data не вплинуть на оригінальний об’єкт defaultData.
I don't know if this is expected behavior or not. But it feels off for me.
It will cause issues when using defaultData multiple times.
(ex. multiple lowdb instances using the same defaultData)
ISSUES:
defaultData
is used as a reference fordata
inlowdb
object.data
inlowdb
is updated thedefaultData
object content is also updated.SOLUTION:
structuredClone
fixed it.The text was updated successfully, but these errors were encountered: