hi,
I have connected two databases with a reference field; Apartments and Clients. Basically each apartment will have one or more clients. So when i click the apartment name it will show all the clients related to it. I intend to fill the clients names using a form input. But i cannot connect the reference field; Apartment Name. is there any code to achieve this
one other things - why do I have to download any one of the images before the submit will work.
Thanks for the coding here, though I still have an issue. My case is slightly different in that I'm trying to insert the input from a dropdown of the primary into the reference field of the more detailed database.
I had done it last week, I believe in using your code examples, and got it to work perfect, but the save feature seemed to be having issues, and it didn't save. Since then I've tried various things but thing worked until I got back here. But I still have an issue - using the code below works EXCEPT that the reference field value ends up on the next record of the dataset?
// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import wixData from 'wix-data';
$w.onReady(function () {
//TODO: write your page related code here...
});
let toInsert = "";
export function dropdown6_change(event, $w) {
// Query from dropdown change event
wixData.query("TypeUsed")
.eq("title", $w("#dropdown6").value)
.find()
.then( (results) => {
let firstItem = results.items[0]; //First Item Results
toInsert = {"usedType": firstItem};
wixData.insert("A-UsedItems", toInsert)
.then( (results) => {
let item = results [0];
} )
.catch( (err) => {
let errorMsg = err;
} );
} );
}
it works perfect now.
thank you so much
You might want to add some console.log() calls to debug your code.
Just by looking at it I can see that where you have:
let firstItem = res.items;
where it should be:
let firstItem = res.items[0];
$w.onReady(function () {
//TODO: write your page related code here...
});
import wixData from 'wix-data';
export function button10_click(event, $w) {
wixData.query('APARTMENT-NAME')
.eq('title', $w('#input10').value)
.find()
.then(res => {
let firstItem = res.items;
let toInsert = {
"apartmentName": firstItem,
"title": $w("#input9").value
};
wixData.insert("APARTMENT-CLIENT--NAME", toInsert)
.then((results) => {
let item = results;
})
.catch((err) => {
let errorMsg = err;
});
});
}
i have tried this code but it leaves the reference field blank when submitted
You can query the collection of apartments for the one with the matching name and then use the _id (or the whole item will work as well) for your insert item.
Hi, thank you for that
is there any code to retrieve the _id with a query when i input the Apartment Name with a user input?
thanks
Hey there,
You need to put the _id of the apartment item in the apartment name field. The reference is really just storing the ID. The API documentation of this should be published shortly.
okay i managed to make some progress with this code but i keep getting "Reference is broken".
import wixData from 'wix-data';
// ...
export function button10_click(event, $w) {
let toInsert = {
"apartmentName": $w("#input10").value,
"title": $w("#input9").value
};
wixData.insert("APARTMENT-CLIENT--NAME", toInsert)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );
}