Hi,
I’m trying to modelise a system with user-item databases. User has a field pointing on item id. But I can’t use the item._id as primary key … So, as mentionned sometimes in this forum, I decided to use the item._id to fill an other field as primary key, item.my_id .
So I insert my item using wixData.insert then it returns the item filled with _id (and other fields hidden in the database).
Then, I try to get my newly inserted item with wixData.get , updating the field my_id = _id then update it in base using wixData.update.
It works. 9 times out of 10. Sometimes, wixData.get(“MyDatabase”, item[“_id”]) returns null object in the Promise, it looks like the item isn’t inserted yet, but I have an _id … No sense, I don’t know with it fails randomly.
I tried an other trick using database hooks and after_insert,
item.my_id = item._id;
from the API, but it doesn’t do anything. My_id still empty. (see here : https://www.wix.com/corvid/reference/wix-data.Hooks.html#afterInsert )
Is there something wrong with dabases and wix ??? Am I doing bad code ? New with JS but Python expert. I can push my code below to be more understandable.
wixData.insert(“MyDatabase”, data) // Insertion
.then( (results) => {
console.log(results); //Well inserted Item
console.log(results[“_id”]); //_id is always correct
wixData.get(“MyDatabase”, result[“_id”]) //Get the item
.then( (item) => {
console.log(item); //9/10 the item, but sometimes null
item.my_id = results[“_id”];
wixData.update(“MyDatabase”, item) // Updating my_id
.then( () => {
console.log(“Custom ID Primary key OK”);
})
catch ( () => {
console.log(‘Error updating id data’);
})
})
. catch ( () => {
console.log(‘Error getting id data’);
})
})
. catch ( () => {
console.log(‘Error inserting data’);
})
In your code snippet, you have .then(result) after the wixData.insert, but the console.log statements reference “results” not “result”. Is this a typo from copying your code or is this really in the actually code? I have also found that it is more reliable to assign the promise results to a variable rather than referencing the promise results themselves.
Yes it’s a typo, I fixed that.
Why assign a promise into a variable should be more reliable ?
It looks like this schema :
New item ----------> Promise ---------> Database
| /\
-----------------------------------|
Maybe the item is created but not yet inserted in the database when I call it after the promise return ?
I can try a dirty fix with a timeout of 100 ms before calling the item in the batabase, but I want to understand why my code works only 90% of the time.
You are nesting promises – something I always have problems getting to work all the time. It may be most of the time the promises resolve in the correct order, but every so often they don’t. You might consider using async/await syntax, as this will make sure that things work in the right order, especially when you are dependent upon a database insert to drive additional processing.
@davebogan98 But when it fails, _ID have a value, that’s strange
Well, thanks @Dave for helping me, I tried with async/await and it works perfectly (and so much easier and clear than .then.catch syntax.
Here is the code updated if someone needs one day.
const inserted = await wixData.insert(“MyDatabase”, datas) // Insertion
const item = await wixData.get(“MyDatabase”, inserted[“_id”]) // Getting the new item
item.my_id= inserted[“_id”];
wixData.update(“MyDatabase”, item) // Updating the custom ID