reference field

Hi,

I have a dataset with 2 fields: “answer” (text field) and “Member” (reference field):

On my site, the user write an answer to a question and the answer goes to the text field “answer”:

I want that the reference field - “Members” - will automatically be set to the current user id/or email (my key field on the members dataset)

how can I do that?
Thanks

Just add another line under toInsert
“member” : userId

Thanks Mike.

I try it and got: “Reference is broken”.
Why?

The reference field is broken because the data cannot display there since it’s reference to fields from a different database… However, you can set the value in one of the field your referencing from.

You can access the Reference field " Field" >> {itemData.members.field key from reference field;}.
then you can use setFieldValue to set the Field you want. “little complicated but i hope you understand”

$w(“#myDataset”).setFieldValue(“title”, “New Title”);

I didn’t understand where to insert this row:
{itemData.members.field key from reference field;}.

And also what is the “title” and “new title” stand for?

Q1 = the DB with reference field called “member”
the referene field “member” is point to DB call “Members”

What shpuld I do? >>

This is my code:

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
import wixCRM from ‘wix-crm’;

$w.onReady( function () {

let user = wixUsers.currentUser;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true

$w(“#sendAnswer”).onClick( () => {

let toInsert = {
“answer”: $w(“#answerText”).value,
“member”: userId,
}

wixData.insert(“Q1”, toInsert)
.then( () => {
wixLocation.to(“/q1”);
} )

. catch ( (err) => {
let errorMsg = err;
$w(“#error”).show();
} );

})
});

1 Like

@ssivan84 Have you been able to figure this out? I am having a similar issue with my code and no one on the forum responds.

1 Like

Following - I have the same issue too. It seems simple, but I just can´t figure out how to retrieve and insert the referenced item ID
I find a lot of explanations and code snippets, but actually, i need to see the whole page code to see what is going on

Any resolution on this (seemingly simple) DB coding action?