Insert user.id to database (code)

Hi!
When the user presses button2 they’re submitting a listing form. When the user presses button2 I want their user.id saved to “dataset1” in the column numUser.
Right now, nothing is saving to the column numUser in the database. How do I fix this?

This is my lightbox code:

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

export function button2_click(event){
 let dataset1 = {
        numUser= user.id,
    }
    wixData.insert("dataset1", dataset1)
} 

I don’t have any backend code.

I’m also getting an error under numUser

Thanks!

Ok, here we go:

  1. this code
numUser= user.id

has a var “user” falling out of the sky. Please lookup docs on wix-users, the answer is out there (let currentuser = …, etc)
2) why would you want to do this? Wix already does this for you, in the _owner field per row

Hi! What do you mean by owner fied?

try this

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

let user = wixUsers.currentUser
let userID = user.id

$w.onReady(function () {
});

export function button2_click(event){
 let tosave = {'fieldname': userID}
    wixData.insert("dataset",tosave)
} 

@amelia3308 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.

@giri-zano 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 EVERYONE FOR THE HELP

@amelia3308 Good on you!

tried using this code on a dynamic page hoping it would fill the field in that repeater’s row in the database when the button on that container is pressed, but it didn’t work. Do you know how to code it into a repeater ?

When i try to use this to insert into a Referenced column, I get a “Broken Reference” entry

@bjamaspi
You should better open your own POST and describe your problematic situation, instead of bumping up old posts.