I’m trying to save field data from one collection into another by using a data query from one dataset (A) and setFieldValue on another dataset (B) within the same function.
I think my code is close because the console log is showing the right data but it’s not saving the result to dataset B yet; can anyone make a suggestion?
export function button_click(event, $w) {
moveData();
}
export function moveData() {
wixData.query('dataset1')
.hasAll("field1", $w("#dropdown1").value)
.hasAll("field2", $w("#dropdown2").value)
.find()
.then((results) => {
let result = results.items[0].title
console.log(result)
$w('dataset2').setFieldValue("newDataField", {result})
})
}
You need to save() the dataset item. Of course, you also need to make sure that both the collection and the dataset have the correct permissions.
Is the field newDataField an object or text? If it’s text, then your statement should look like this (without the curly brackets):
$w('dataset2').setFieldValue("newDataField", result);
Unfortunately this still doesn’t work. I have given both dataset A and B read and write permissions (typically A was read only, and B was write only).
I have written a similar function that creates a unique code it saves to dataset B. Here is my updated code;
export function button_click(event, $w) {
createCode();
moveData();
$w('dataset2').save();
}
export function createCode() {
let newCode = "CBF" + Math.random().toString(36).substring(2,6).toUpperCase()
return $w('#dataset2').setFieldValue("uniqueCode", newCode)
}
export function moveData() {
wixData.query('dataset1')
.hasAll("field1", $w("dropdown1").value)
.hasAll("field2", $w("dropdown2").value)
.find()
.then((results) => {
let result = results.items[0].title
console.log(result)
return $w('dataset2').setFieldValue("newDataField", result)
})
}
createCode() works as expected; it creates a random 7 character string and saves it to “uniqueCode” in dataset B when button_click is triggered.
moveData() just saves a blank space to “newDataField” in dataset B.
Is the problem that I can’t make a db query from one dataset and then save it to another within the same function? Any thoughts?
Regarding this line of code in the moveData() function:
wixData.query('dataset1')
Do you actually have a database collection called dataset1? Or are you trying to perform a query on on a Dataset called dataset1?
The wix-data API does not work on Datasets. To work with Datasets, you need to use the wix-dataset API.
Yisrael, pay atttention…
This was your suggestion…
$w('dataset2').setFieldValue("newDataField",result);
…and the post-opener follows your suggestion. —>#
Here an example…
exportfunction moveData(){ wixData.query('dataset1').hasAll("field1",$w("dropdown1").value).hasAll("field2",$w("dropdown2").value)
2x missing → #
I’m pulling in 2 data collections from content manager as $w elements on page. The default wix # values are #dataset1 and #dataset2.
Is there a different way using code to save to a data collection without using $w(“#dataset”).saveFieldValue(“fieldKey”, value) ?
I apologize for the bad syntax, I wrote the functions out instead of copy/paste from my IDE.
I’m now trying an async/await approach to this problem but having no more success than any of the other above examples I have given.
Here is my updated code.
export async function button_click(event, $w) {
createCode();
let number = await moveData()
// this will console log undefined
console.log(number)
$w('#dataset2').setFieldValue("newDataField", number)
$w('#dataset2').save();
}
// this works as expected and saves a 7 character code to uniqueCode in #dataset2
export function createCode() {
let newCode = "CBF" + Math.random().toString(36).substring(2,6).toUpperCase()
return $w('#dataset2').setFieldValue("uniqueCode", newCode)
}
// this will console log the right value for result in the inspector
export function moveData() {
wixData.query('#dataset1')
.hasAll("field1", $w("#dropdown1").value)
.hasAll("field2", $w("#dropdown2").value)
.find()
.then((results) => {
if (results.totalCount > 0) {
let result = results.items[0].title
console.log(result)
return result
} else {
return "No Results"
}
})
.catch((err) => console.log(err));
}
The crazy part is the console.log(result) in moveData() actually prints the value I’m looking to save in dataset B.
I’m making some progress as the console.log(number) in button_click is returning undefined, any thoughts on that?
I thought about writing a conditional on the button_click like the code below but I’m introducing new errors with truthiness and confusing myself a bit too much.
export async function button_click(event, $w) {
createCode();
let number = await moveData()
// This condition will always return 'false' since the types 'void' and 'string' have no overlap.
if (number === "No Results) {
console.log("BAD DATA OR NO DATA BACK")
} else {
console.log(number)
$w('#dataset2').setFieldValue("newDataField", number)
}
$w('#dataset2').save();
}
I feel like I’m getting close with this but I’m just missing something critical to saving the value to the new dataset, can anyone help?