I would like to be able to chain 4 insert statements together, where each subsequent insert statement uses the result of the previous insert statement, in particular the id of the previously inserted record.
However, even though I am making use of the .then(), the second Promise in the chain does not resolve before the code in the second .then() runs. What am I missing?
wixData.insert(“Student”, insertArray)
.then( (results) => {
let item = results; //item that was just inserted
studentId = item._id;
firstLastAge = item.firstName+item.lastName+item.age;
toInsert = {“mid”: userID, “chid”: item._id, “studentName”: item.firstName + " " + item.lastName};
wixData.insert(“ParentProfile”, toInsert)
})
.then( (resultPP) => {
let item = resultPP;
ppId = resultPP._id;
wixData.insert(“StudentProfile”, toInsert)
})
.then( resultSP => {
let item = resultSP;
spId = resultSP._id;
let masterRec = {“firstLastAge”: firstLastAge, “studentId” : studentId, “parentProfile”: ppId, “studentProfile”: spId};
wixData.insert(“ProfileMaster”, masterRec)
})
.then( (resultPM) => {
let item = resultPM;
})
.catch( (err1) => {
console.error("error during chained insert instructions: " + err1);
});