logged in user read and write page

Hi I have created a user submission form which captures all fields into a database collection.

I have also created a duplicate page to enable user review of all entries made however cannot get the page to show the current users entries, using a dataset, setup with read and write.

I have filtered the dataset to include logged in users but all fields show as empty i.e. requiring inputs again

i had previously used Wix data query but couldn’t get this to work either and now i’m completely lost and stuck

please help :slight_smile:

Have a look at using Wix Dataset API and the getCurrentItem function, although if the user is logged in then you can easily get their userid or email for example and simply use Wix Data query function of eq to match from your dataset and get only their info.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem

$w("#input1").value = $w('#dynamicDataset').getCurrentItem();
// Gets all the fields data for that user.
 
$w("#input1").value = $w("#dataset1").getCurrentItem().fieldName;
// Gets only the specified field for that user.

Hi i’ve tried this which doesn’t actually work, although i know its my mistake

import wixData from ‘wix-data’;
wixData.query(“memberTextData”)
$w(“#dropdown6”).value = $w(‘#memberTextData’).getCurrentItem();
.find()
.then( (results) => {
console.log(results.items);
} );

Note: dropdown 6 is one of the form fields on the page and member text data is the collection

Thanks
Adam

That won’t work as for starters you are missing your page onReady function underneath the import line.

Plus, either you need a event handler to run the query or to write it into your page onReady function so that it runs when the page loads.

Have a look at this tutorial here as it keeps it all on the same page.
https://www.vorbly.com/Vorbly-Code/WIX-SUBMIT-FORM-AND-UPDATE-REPEATER

Finally, if you are calling something from a dataset, then just do it like in this tutorial from the same website.
https://www.vorbly.com/Vorbly-Code/WIX-CODE-AUTO-FILL-FORM-WITH-USER-INPUTS

Also, if you do use getCurrentItem, then make sure that you have the dataset wrapped up in the page onReady function with it’s own onReady function.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem

Hey sorry its not making sense tonight have tried this

$w.onReady( function () {
$w.onReady( () => {
$w(“#dataset5”).getCurrentItem();
let itemObj = $w(“#dataset5”).getCurrentItem();

} );

} );

@adcatch02

You can’t have two page onReady functions like that, use one or the other.
https://www.wix.com/corvid/reference/$w.html#onReady

Read the API reference for getCurrentItem, it gives you code examples…

Get the dataset’s current item when the page loads

$w.onReady( () => {
  $w("#myDataset").onReady( () => {
    let itemObj = $w("#myDataset").getCurrentItem();

  } );

} );

@givemeawhisky
Thanks so i think it will work

I think i know where now that this isn’t working, the initial collection just isn’t saving as it was before. I am not seeing any logs in the console as should be happening and was happening. I think i’ll need to raise a ticket because the code is long on this page

import {local} from ‘wix-storage’;
import wixLocation from “wix-location”;
import wixData from ‘wix-data’;

export function button22_click(event) {
$w(‘#columnStrip12’).collapse();
$w(‘#columnStrip8’).expand();
}

export function dropdown4_change(event) {
$w(‘#button25’).expand();
}
export function button25_click(event) {
$w(‘#columnStrip8’).collapse();
$w(‘#columnStrip1’).expand();
}

export function button24_click(event) {
$w(‘#columnStrip1’).collapse();
$w(‘#columnStrip9’).expand();
}

export function vectorImage3_click(event) {
$w(‘#box9’).expand();
$w(‘#vectorImage2’).expand();
}

export function vectorImage2_click(event) {
$w(‘#box9’).collapse();
}

export function dropdown1_change(event) {
$w(‘#button23’).expand();
}
export function button23_click(event) {
$w(‘#columnStrip9’).collapse();
$w(‘#columnStrip11’).expand();
}

//photo upload code
export function uploadButton4_change(event) {
$w(‘#button19’).expand();
$w(‘#text80’).hide();
$w(‘#text96’).expand();
}

export function button19_click(event) {
if ($w(“#uploadButton4”).value.length > 0) {
$w(“#text96”).text = Uploading ${$w("#uploadButton4").value[0].name};
$w(‘#image44’).show();
$w(“#uploadButton4”).startUpload()
.then( (uploadedFile) => {
let toInsert =
{“photo”:
uploadedFile.url };
$w(“#text96”).text = “Upload successful”;
$w(‘#text96’).collapse();
$w(‘#image44’).hide();
$w(‘#text96’).expand();
$w(“#uploadButton4”).disable();
$w(‘#button21’).show();
$w(“#image46”).src = uploadedFile.url;

wixData.insert(“TextData”, toInsert)
.then( (results) => {
let item = results; //see item below

} )
. catch ( (err) => {
let errorMsg = err;
} );

  }) 
  . **catch** ( (uploadError) => { 
    $w("#text96").text = "File upload error"; 

  }); 

}
else {
$w(“#text96”).text = “Please choose a file to upload.”;
}

$w(‘#button19’).hide();

}

export function button1_click(event) {
$w(‘#columnStrip11’).collapse();
$w(‘#columnStrip10’).expand();
}

//change element IDs and Field Keys as necessary for you’re specific setup
export function button21_click(event) {

$w(‘#button21’).onClick( function () {

console.log(‘i am slow, give me a few more seconds to fetch the image URL’);
$w(“#uploadButton4”).startUpload()
.then( (imageUploaded) => {

let imageURL = imageUploaded.url

      console.log(imageURL); 

let toInsert = {

// consider also uploading the registered users ID or Email Address so that you can identify who uploaded the information

“relationship”: $w(‘#dropdown6’).value,
“field”: $w(‘#dropdown4’).value,
“firstName”: $w(‘#input1’).value,
“lastName”: $w(‘#input2’).value,
“dateOfBirth”: $w(‘#input3’).value,
“dateOfDeath”: $w(‘#input4’).value,
“nickName”: $w(‘#input5’).value,
“createdBy”: $w(‘#input6’).value,
“composureAssistant”: $w(‘#dropdown1’).value,
“post”: $w(‘#richTextBox1’).value,
“photo”: imageURL

}; 


            wixData.insert("TextData", toInsert)  
                .then((results) => { 

                    console.log("user input saved to database"); 


              }) 

}) 
}) 

wixLocation.to(“/memberpersonalisereview”);
console.log(“/memberpersonalisereview”);

}

Thanks
Adam

Oh I have reverted to a previous site version. I still can’t seem to save the data from the previous page which is the code above into the collection as a single set of data in one row, its saving the image but not the text entries

when the previous page saves and locates to the review page i have this code,

import wixData from ‘wix-data’;
wixData.query(“TextData”)
.eq(“iD”. userId)
.limit(1)
.find()
.then( (results) => {
let firstItem = results.items[0];
} )
. catch ( (err) => { let errorMsg = err; } );
$w.onReady( () => {
$w(“#dataset5”).onReady( () => {
$w(“#dataset5”).revert()
.then( () => {
console.log(“Done reverting the item”);
} )
. catch ( (err) => {
let errMsg = err;
} );

} );

} );

I have used the revert function to pull the last row from the collection

I am also getting this error message which is
line 4 - wixData.query(“TextData”)

so on the review page i have added this code

import wixUsers from ‘wix-users’;

import wixData from ‘wix-data’;
wixData.query(“TextData”)
.eq(“iD”. userId)
.limit(1)
.find()
.then( (results) => {
let firstItem = results.items[0];
} )
. catch ( (err) => { let errorMsg = err; } );
$w.onReady( () => {
$w(“#dropdown6”).value = $w(‘#dataset5’).getCurrentItem();

  } ); 

but on the initial form page prior to this one i cannot get the collection to save properly

Your Wix Data Query is wrong with the eq function.

.eq("iD". userId)

It should be like this.

.eq("_id", userId) 

See here for more info about system fields.
CMS (Formerly Content Manager): About Your Collection Fields | Help Center | Wix.com

Also, with your code, the page and the dataset onReady functions should be underneath your imports with the data query underneath that.

You need to make sure that the page and the dataset has fully loaded before you run your query on the dataset.

ok we are getting there. a problem with this line wixData.query(“TextData”) though

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

$w.onReady( () => {
$w(“#dataset5”).onReady( () => {
$w(“#dataset5”).revert()
.then( () => {
console.log(“Done reverting the item”);
} )
. catch ( (err) => {
let errMsg = err;
} );

} );

} );

wixData.query(“TextData”)
.eq(“id”. userId)
.limit(1)
.find()
.then( (results) => {
let firstItem = results.items[0];
} )
. catch ( (err) => { let errorMsg = err; } );

Thanks Adam