I have various stages of capturing a users data. Upon the user submitting data in the first stage I want to retrieve the ID of the newly inserted row and store it in the session so I can use it to update the row later on in the process.
let toInsert = {
postcode : $w("#pcinput").value
};
wixData.insert("dataset1", toInsert)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );
I believe I need to use afterinsert, but where does this go and what would it look like in order that the _id is returned?
The insert() function returns the item that was added. Therefore results._id is the ID of the item that was added.
See the insert() API for details.
Got it, thanks - am I saving correctly to session in the following?:
let toInsert = {
postcode : $w("#pcinput").value
};
wixData.insert("mydataset", toInsert)
.then( (results) => {
console.log(results._id);
session.setItem("sessionid", results._id);
} )
.catch( (err) => {
let errorMsg = err;
} );
@iaindowner give it a try, it seems OK - although I haven’t tested the code
I tried this code and it does not return the _id. Any reason why? I need to get the _id to update the dataset and stay on one line.
This post is old and is being closed.
Please add your own issue into a new post instead of bumping up an old post.
Also, add any code in a code block as stated in the Forum Guidelines. Explain what you’re trying to do, what works, and what doesn’t.