Hello!
I have been trying for a long time to add new entries to a collection using input forms. So far, the ways I have tested have only lead to already existing collection entries becoming updated.
*I have tested the built in “Submit” and “New” actions.
*I have tried using I have also tried to write my own code.
I would be very thankful if someone could point me in any new direction.
My goal is that I want users to upload text pieces and I want to be able to show that on the page, one (or perhaps a few) at a time, sorted by topic if possible.
Thank you very much!
Part of code:
export function button1_click(event) {
let textToInsert = $w("#input1").value;
let topicToInsert = $w("#input2").value;
let toInsertBoth = {
"prayer": textToInsert,
"topic": topicToInsert ,
};
let toInsertTopic = {
"title": textToInsert,
"topic": topicToInsert ,
};
wixData.insert('PrayersInHeaven', toInsertBoth);
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );
// Runs a query on the "recipes" collection
wixData.query("PrayersInHeavenTopics")
// Query the collection for any items whose "Name" field contains
// the value the user entered in the input element
.contains("topic", topicToInsert)
.find()
.then( (results) => {
if(results.items.length == 0) {
wixData.insert('PrayersInHeavenTopics', toInsertTopic)
} else {
// handle case where no matching items found
}
} )
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
} );