broken data on load app
aldobel opened this issue · comments
Hi, congratulations on the library, I hope you continue to carry out the project. First, I hope the documentation will be completed, as there are still a few features missing. however, the problem that I expose (perhaps due to my inexperience), after I save an object in the database {name: 'name', url: 'url', playlist: [{}]}
, when I reopen the app, in the playlist I see:
{name: 'name', url: 'url', playlist: [object Object], [object Object], [object Object]}
, and I cannot retrieve the data.
I hope for a resolution. Thanks
Welcome, @aldobel.
Did you wrap the fetch function inside onLoaded
event?
For example:
componentDidMout() {
yourdb.onLoaded(this.fetchPlaylists)
}
fetchPlaylists = () => {
// fetch playlist code
}
Thanks for the reply. Unfortunately I have already tried with this code:
useEffect(() => {
Liste.onLoaded(() => setValueListe(lis()));
}, []);
const lis = () => {
Liste.data();
};
but the answer is always this:
[
{
"id": "61a69ac2f0MKO9fk0009p6bJ",
"namePlaylist": "Lista",
"urlPlaylist": "url",
"live": "[object Object],[object Object],[object Object],[object Object],[object Object]",
}
]
Can you show your data schema? Do you use reference data type
for live
?
(Note that it doesn't support nested objects at the moment)
This is my Schema:
export class listSchema {
name = "Liste";
props = {
namePlaylist: "string",
urlPlaylist: "string",
live: [],
};
}
I must therefore specify the object that will go live, for example:
export class listSchema {
name = "Liste";
props = {
namePlaylist: "string",
urlPlaylist: "string",
live: [{
name: 'string',
info: '?string'
}],
};
}
Thanks!
Ok, since vasern isn't support nested object yet, you'll need to have another schema and use it as a reference. For example
class Item {
... // Schema code
}
export class listSchema {
name = "Liste";
props = {
namePlaylist: "string",
urlPlaylist: "string",
// [] is for list/array
// # is for reference
live: "[]#Item"
};
}
I just did this, but I don't know if I got something wrong:
class Item {
name = "Item";
props = {
group: "?string",
url: "?string",
name: "?string",
logo: "?string",
};
}
export class listSchema {
name = "Liste";
props = {
namePlaylist: "string",
urlPlaylist: "string",
live: "[]#Item",
};
}
const VasernDB = new Vasern({
schemas: [listSchema],
version: 1,
});
Now he gives me:
{
"id": "61a7b6b8gprd0pSF000tXqjv",
"namePlaylist": "Lista 2",
"urlPlaylist": "url",
"live_id": ["" ],
}
This version of Vasern is still a bit handy:
- You need to pass all schema when creating a new Vasern instance
- When getting the results, a list of references (
[]#Item
in this case) will only return ids of the item. You'll need to manually get items from theItem
collection using thefilter
function
The following code should works
class ItemSchema {
name = "Item";
props = {
group: "?string",
url: "?string",
name: "?string",
logo: "?string",
};
}
class ListSchema {
name = "List";
props = {
name: "string",
url: "string",
items: "[]#Item",
};
}
const VasernDB = new Vasern({
schemas: [ListSchema, ItemSchema], // all schema here
version: 1,
});
const { List, Item } = VasernDB;
// Insert items and create a list
const insertItems = Item.insert([{...}]);
const insertedLists = List.insert({
name: "List 1",
url: "list/url",
items: insertedItems
})
// Get data
VasernDB.onLoaded(() => {
const firstList = List.data()[0];
// Need to get items from Item collection using filter
const firstListItems = Item.filter(item => firstList.items.indexOf(item.id) !== -1).data();
console.log({ list: firstList, items: firstListItems })
})
By the way, Vasern is still pretty much a work-in-progress and I haven't been able to work on Vasern for some time. I'd highly recommend using an alternative (WatermelonDB, Realm, etc)
Ok, as soon as I can I try and let you know. A thousand thanks