Hi everyone ! So, i have a collection of cities and countries, each city is stored in “title” field and each country is stored in “country” field. the dataset ID on my page is “cities”.
I would like to automaticall store, in another database, the country related to the city that the user put in the input #Depart. So, here is my code :
export function button11_click(event) {
wixData.query("cities")
.eq("title", $w("#Depart").value)
.find()
.then( (results) => {
let departure = results.items ;
//The supposedly problematic line :
let depcountry = departure.country
console.log(depcountry);
$w("#dynamicDataset").setFieldValues({
"paysDeDepartBrouillon" : depcountry})
$w("#dynamicDataset").save()
})
.catch( (err) => {
let errorMsg = err;
} );
The console send “Undefined”.
Thank you !
Hi there,
I’d suggest logging the departure variable on its own. You’ll see you get an output of an array with all your items. I’ve created an example for demonstration:
Because of this, departure.country will be undefined since it’s not sure which item from the array you’re trying to output. In this case, you can specify the item by its index like so:
As you can see I was able to log the output in my console. If you wanted to output all countries you’ll most likely need to loop through the array and output the items.
I hope this was helpful!
Best regards,
Miguel
Miguel, you are a savior !! Thank you, it works !
However, now that depcountry is defined, I don’t understand why it doesn’t implement in the other database using this command (as shown on the code below) :
//...
let depcountry = departure.country
console.log(depcountry);
$w("#dynamicDataset").setFieldValues({
"paysDeDepartBrouillon" : depcountry})
$w("#dynamicDataset").save()
})
.catch( (err) => {
let errorMsg = err;
} );
Miguel, you are a savior !! Thank you, it works ! However, now that depcountry is defined, I don’t understand why it doesn’t implement in the other database using this command (as shown on the code below) :
//...
let depcountry = departure.country
console.log(depcountry);
$w("#dynamicDataset").setFieldValues({
"paysDeDepartBrouillon" : depcountry})
$w("#dynamicDataset").save()
})
.catch( (err) => {
let errorMsg = err;
} );