Data Collection -- user input from multiple pages in same row?

I have created a data collection, and connected user input to it from multiple pages. I would like all the data from one session to end up in the same row of the collection (each page writes to different fields). How do I do this? If I submit from a button or save from the code, the data from each page ends up in its own row.

1 Like

Will this collection use multiple rows, or is the entire collection intended to be 1 row?
If multiple rows are possible, you’ll first need some criteria for the 2 pages to know the data needs to be on the same row, likely using wixData.query() or possibly wixData.get() if you have a known ._id value for that row.

Once you have the initial row data, you can freely modify that object in the 2nd page. Aftwards, use wixData.update() to push the modified data back into the database.

Deciding how to determine the 2 pages should be sycning up on this row would likely be the hardest part, depending on your application.

Disclaimer: Untested generic code.

function PageOne()
{
let newEntry = {
‘_id’ : ‘123’ ,
‘data1’ : ‘DataFromPage1’ ,
}

wixData.insert( 'MyDatabase' , newEntry); 

}

function PageTwo()
{
wixData.get( ‘MyDatabase’ , ‘123’ )
.then( results => {
results.data2 = ‘DataFromPage2’ ;
wixData.update( ‘MyDatabase’ , results);
})

}

Thank you, this is what I was looking for. I’ve seen that if I do the first insert without specifying an id, a new entry will be generated automatically.

I’m relatively new to java – is newEntry a dictionary? How can I get the value of _id from newEntry after calling insert()?

I’m thinking I will use the afterInsert() hook to read and store the id using session.setItem(). Then the other pages can do session.getItem() and wixData.get().

Is there an easier way of going about this?

newEntry was just the variable name I chose; it has no significance.

Using session data seems reasonable, if you want 1 user to be putting data in from each page.

After using insert, it returns the object that was inserted. You can get the ID value from there. You don’t need to use an afterInsert().

Keep in mind that if the user can enter the page data in either order, each page will need to have both functionality. Possibly check if the session data exists to decide if you need to generate a new entry, or modify an existing one.

Again, generic untested code - just to use for reference :slightly_smiling_face:

import wixData from ‘wix-data’ ;
import {session} from ‘wix-storage’ ;

function PageOne()
{
// Create an object to be entered into the database.
let newEntry = {
‘data1’ : ‘DataFromPage1’ ,
}

// Insert the object into the database.
wixData.insert( ‘MyDatabase’ , newEntry)
.then( result => {
// Get the ID that was just inserted.
let myID = result._id;

// Save the ID to session data.
session.setItem( ‘userDatabaseID’ , myID );
})
}

function PageTwo()
{
// Get the ID from session data.
let myID = session.getItem( ‘userDatabaseID’ );

// Get the object from the database.
wixData.get( ‘MyDatabase’ , myID)
.then( results => {
// Modify the object.
results.data2 = ‘DataFromPage2’ ;

// Update the modified object back into the database.
wixData.update( ‘MyDatabase’ , results);
})
}