I’ve been trying for hours to do this and I cannot figure it out. This is something that is so simple to do in nearly any db framework, and I’m constantly hitting a wall…
I have a collection of survey info which has a reference field to the users collection. All I want to do is create a form and INPUT the logged in user INTO the survey collections USER reference field.
Here is my code:
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
let currentUser = wixUsers.currentUser;
export function button2_click(event) {
//Add your code for this event here:
let toInsert = {
“title”: $w(“#input1”).value,
“content”: $w(“#input1”).value,
“satisfaction”: $w(“#input1”).value,
“user”: currentUser.id //THIS IS THE REFERENCE FIELD
};
wixData.insert(“Survey”, toInsert)
.then((results) => {
let item = results; //see item below
console.log(item)
})
. catch ((err) => {
let errorMsg = err;
});
}
I have even tried hard coding that user value with an id from the user collection and it still does not work. I don’t get any errors, the collection updates and the user reference field is blank.
I have also tried hard coding values using the insertReference function and that also does NOT work.
Any help would greatly be appreciated!
Hello Elliot,
i don’t know if i understood your problem at all, because my english ist not so good, but i think this code-snipet perhaps can help you a bit.
$w("#myDataset").onReady( () => {
let toInsert = {
"title": $w("#input1").value
"content": $w("#input2").value,
"_owner": $w("#input3").value, //------> (owner-ID) <<<<<<-------------- i think this is waht YOU NEED !
"_id": $w("#input4").value, //------> (unique Item-ID)
};
wixData.insert("myDataCollection", toInsert).then( (results) => {
let item = results;
} )
})
To see if someone is logged in or not, you can see it in the Dataset-Collection at “_owner” (do not forget the underline —> _owner !!!
The same you will find at the hidden field called “_id”.
Sorry for my bad english.
Hey Dima,
Thanks for the reply. I think you’re english is good, so no worries! I guess what I am asking is that I need to directly insert into the reference table in my survey collection from data from my form.
The reference field in my survey collection is called “user”, and I am trying to input the owner id into that field upon the button clicking. The .insert function already automatically inserts the _owner id info, so I don’t have to code it in. My user reference field stays blank no matter what I try.
Sorry, but hard for me to understand your problem because i can’t see what you see., so here a few questions…
I’m using the “Reference” field type in the collections page. This is supposed to create a dynamic reference between two collections. It’s the field I’m trying to insert into in my form.
I figured it out. OK for those searching and wondering how this is done, since the API nor the tech docs explain this behavior…
When you are in your collections screen and you select “reference” type from the field values, IF “Multiple Items” is checked, you WILL NOT be able to write to this table via the code editor. If its unchecked, you can insert as normal…
If you want “Multiple Items” on, you’ll need to run the wixData.insertReference function in the returned promise of the original insert. It needs to be in the returned promise body, because the function NEEDS the _id of the item you just created in order to complete the insertion. I just saved you 6 hours of needless headaches.
Something like this… Ignore the variable names.
let toInsert = {
"title": "Tom Bodat",
"newField2": "fb843da4-b3a9-452f-820e-7d1d108ca03e"
};
wixData.insert("Actors", toInsert)
.then((results) => {
let item = results; //see item below
console.log(item._id)
wixData.insertReference("Actors", "test",item._id, currentUser.id )
.then(() => {
console.log("Reference inserted");
})
.catch((error) => {
console.log(error);
});
})
.catch((err) => {
let errorMsg = err;
});
Did you get the solution ? I’m also looking for inserting a reference in a database.
Did you get the solution ? I’m also looking for inserting a reference in a database.
The solution (as mentioned by Elliot Grey ) you can find in the second post-answer.