Use query to put inputted info into column?

Hi! I am having hard time using queries.
I have read a lot of websites and forum posts but I don’t know how to accomplish what I’m trying to do.

I am trying to query my database “dataset1” and then input user.id (the user’s id) into my column “userId” as the user presses submit on a lightbox.

This is my lightbox code. I don’t have any backend code.

import wixData from 'wix-data';
import wixUsers from 'wix-users';
let user = wixUsers.currentUser;
let person = user.id;

    wixData.query("dataset1").eq('userId', userId).find()
        .then(result => {
            let dataset1 = result.items[current]; 
            dataset1.userID = $w(person).value
            wixData.save("dataset1", dataset1);
        });

I don’t know how to use the code below. I just guessed on how to input things. The first line is supposed to get the dataset and the current data piece that the user-created by submitting the lightbox. Second-line is supposed to set the column userID to their user.id called person. Third line is supposed to save those changes.

            let dataset1 = result.items[current]; 
            dataset1.userID = $w(person).value
            wixData.save("dataset1", dataset1);

I am also getting an error saying userId is not defined though I don’t know why. It also says [current] is not defined which makes sense because I guessed.

Please help the query corvid instruction sites are very confusing to me and I’m having a very hard time.

Hai Melia S

In your code change this →

let person = user.id;

into →

let userId = user.id;

It will work !!

For our main code -

Add this to the first of our code →

import wixData from 'wix-data';
import wixUsers from 'wix-users';

Then add a onclick function to the button.
To add an onclick function to the submit button.

  1. Click on the button
  2. In the properties panel and click onclick function. →

Under the onclick function add this code -

wixData.query("YourCollectionName")
  .eq("Your field key", userId)
  .find()
.then( (results) => {
   if(results.items.length > 0) {
    let items = results.items;
    let toSave = {
 "title":        "Mr.",
  "userId":   userId,
  "last_name":    "Doe"
};
wixData.save("YourCollection", toSave)
	.then( (results1) => {
		let item = results1;
	} )
	.catch( (err) => {
		let errorMsg = err;
	} );
    } else {

      // handle case where no matching items found
    }

  } )

  .catch( (error) => {

    let errorMsg = error.message;

    let code = error.code;

  } );

Final code →

// For full API documentation, including code examples, visit https://wix.to/94BuAAs
import wixData from 'wix-data';
import wixUsers from 'wix-users';
$w.onReady(function () {
 // TODO: write your page related code here...

});

export function button1_click(event) {
let user = wixUsers.currentUser;
let userId = user.id;

    wixData.query("YourCollectionName")

  .eq("Your field key", userId)

  .find()
.then( (results) => {

 if(results.items.length > 0) {

 let items = results.items;
 let toSave = {

 "title":          $w('#input1').value,
 "userId":   userId,
 "last_name":    "Doe"
};

wixData.save("myCollection", toSave)

    .then( (results1) => {

 let item = results1; //see item below

    } )

    .catch( (err) => {

 let errorMsg = err;

    } );

    } else {

 // handle case where no matching items found

    }

  } )

  .catch( (error) => {

 let errorMsg = error.message;

 let code = error.code;

  } );
}

Post if the code works !!

Hi!! Thanks for the reply.

Sorry for the confusion, but I don’t think I should do that.

I’m trying to compare: user.id (the active user’s id) to userId (the user id stored in the database).

Maybe I should have made the words for the current user and the database id stored more different… :sweat_smile:
(I haven’t read the rest of your replies yet, so I’ll do that)

I’ve decided to change my userId column to a different name. I’ll call the column “numUser” instead.

Thank you so much for your reply! Sorry I wasn’t clear on this before, but I want my listing form to input the current person’s user.id to the column “numUser”.

Then, on my page to view a listing, I want the active person’s user.id to compare to the post’s “numUser”.

Here’s my code now:
Unfortunately, user.id isn’t saved to my data column “numUser”

import wixData from 'wix-data';
import wixUsers from 'wix-users';

export function button2_click(event) {
 let user = wixUsers.currentUser;
 let userId = user.id;
    wixData.query("dataset1")
  .eq("numUser", numUser)
  .find()
.then( (results) => {
 if(results.items.length > 0) {
 let items = results.items;
 let toSave = {
 "numUser":   numUser,
    }
   }
    }
    };

I’ve tried another way here: https://www.wix.com/corvid/forum/community-discussion/insert-user-id-to-database-code

But I’m also still trying to figure out this code… that i posted above.

Hai Melia S,

Did you see this bit of code -

let user = wixUsers.currentUser;
 let userId = user.id;

It is assigning the current user id as userId.
So this should be your code

 import wixData from 'wix-data';
import wixUsers from 'wix-users';

export function button2_click(event) {
 let user = wixUsers.currentUser;
 let userId = user.id;
    wixData.query("dataset1")
  .eq("numUser", numUser)
  .find()
.then( (results) => {
 if(results.items.length > 0) {
 let items = results.items;
 let toSave = {
 "numUser":   userId,
    }
   }
    }
    };

Just look at the bold(ed) code…

Ajith

Thanks so much for the help! Now I’m getting an error saying numUser is not defined?

NEVERMIND I DID IT.
I gave up on the code and used @Giri Zano 's advice.

In Wix db, every row inside a collection CAN have an owner column filled. If a Member submits data to a collection, his userId is saved in the row, in the column _owner. Go to Content Manager (you know, the database spreadsheet thing), click on Manage Fields, tick "_owner", and voila.

I added an _owner in manage fields.

Then for comparing the active person’s id to the author id, I used this code

export function dataset2_ready() {
 let user = wixUsers.currentUser;
 let userId = user.id;
 let isLoggedIn = user.loggedIn;

    $w("#input3").value = $w("#text35").text;

    $w.onReady(function () {
        $w("#dynamicDataset").onReady(() => {
 let itemFieldName = $w("#dynamicDataset").getCurrentItem()._owner;
 if (user.id === itemFieldName) {
                        console.log("hello post creator");
                    } else {
                        console.log("you're not the post creator" + itemFieldName + " " + user.id);
                    }
                });
    });
}

OK I DID IT YAY.

I gave up on using code to enter the data and used @giri-zano 's advice

In Wix db, every row inside a collection CAN have an owner column filled. If a Member submits data to a collection, his userId is saved in the row, in the column _owner. Go to Content Manager (you know, the database spreadsheet thing), click on Manage Fields, tick "_owner", and voila.

Then I used this code to compare the active user.id to the poster user.id

export function dataset2_ready() {
 let user = wixUsers.currentUser;
 let userId = user.id;
 let isLoggedIn = user.loggedIn;

    $w("#input3").value = $w("#text35").text;

    $w.onReady(function () {
        $w("#dynamicDataset").onReady(() => {
 let itemFieldName = $w("#dynamicDataset").getCurrentItem()._owner;
 if (user.id === itemFieldName) {
                        console.log("hello post creator");
                    } else {
                        console.log("you're not the post creator" + itemFieldName + " " + user.id);
                    }
                });
    });
}

THANK YOU AJITH THANK YOU EVERYONE FOR THE HELP

:grinning: