Hello,
I’m looking for some help with some code that I’m writing to insert some user entries into a database. please see below.
The data should be entered into the database on the push of a submit button. This should allow a user to create one line in a database with multiple columns. However, when a user resubmits the same data again at a later time, the submit button should update the existing one line in the database and not add an additional line.
Many thanks for the help.
Cheers
import wixUsers from 'wix-users';
import wixData from 'wix-data';
// -------------------------------- Add Exp Button -----------------------------------------------------------
export function addWorkExpClick() {
$w('#inputFields').show();
}
// -------------------------------- Submit Button -----------------------------------------------------------
export function submitButtonClick() {
// Update text input fields and capture data elements in an array
var fieldEntries = [];
for (var i = 1; i <= 3; i++) {
$w('#text' + i).text = $w('#input' + i).value;
fieldEntries.push($w('#input' + i).value);
}
// Define data structure for database input
var dataStructure = function (firstName, lastName, job) {
this.firstName = firstName;
this.lastName = lastName;
this.job = job;
};
// Insert data into database
function module() {
let currentUser = wixUsers.currentUser;
if (currentUser.loggedIn) {
currentUser.getEmail().then(email => {
wixData.query('testData').eq('email', email).find()
.then(result => {
let item = result.items[0];
var toInsert = new dataStructure(item.fieldEntries[0], item.fieldEntries[1], item.fieldEntries[2]);
wixData.insert("testData", toInsert)
.then((results) => {
let firstItem = results; //see item below
console.log(firstItem);
});
});
});
}
}
module();
// Hide button
$w('#inputFields').hide();
}
// End
Hi,
Check out wix-data - Velo API Reference - Wix.com.
If you pass to “save” a (possibly modified) record that you got from the “find” call, it will update the same record.
If the “find” did not return a result, you can pass a totally new object to “save” and it will insert it.
(Looks like the code in your question currently assumes that it will always find a previous record in the DB)
Good luck
Thanks Ofer, I will try that and let you know how it goes.
So I have tried the save function and it is adding items to the database however still not in the place that I’m trying to get it to, please see the image below. The user should be push a submit button and the data should always go to line 3.
Currently the code looks as follows but it inserts/saves data to additional lines every time the submit button is clicked:
// Define data structure for database input
var dataStructure = function (firstName, lastName, job) {
this.firstName = firstName;
this.lastName = lastName;
this.job = job;
};
var toInsert = new dataStructure(fieldEntries[0], fieldEntries[1], fieldEntries[2]);
console.log(toInsert);
// Insert data into database
function module() {
let currentUser = wixUsers.currentUser;
currentUser.getEmail().then(email => {
wixData.query('testData').eq('email', email).find()
.then(result => {
wixData.save("testData", toInsert);
});
});
}
module();
Thanks in advance for the help.
Chris
Hi Chris,
Welcome to content me.
I guess you want to create a submit form with difference type of user-input right??
Heson
Correct let me post a picture, it is just a test for right now but I will be building it out further using this functionality
The fields on the right are user input fields, and the text on the right gets updated when pressing submit. In addition the data should be inserted into the database as described above.
Hi chrs,
Have you try the basic submit form? It’s because I think basic submit form can get the result you want. : )
Would you mind cap the screen let us know correctly what you want to do?
Yes, a basic submit would accomplish the task on this level but I’m looking to add some additional functionality which gets to be a bit more complex than just the submit form. The toInsert variable will at some point become a dynamic variable dependent on other interactions on the page. That is why I’m trying to get it to work via code.
Hi Chris,
In order for wixData to update an existing record in “save”, the record you insert should have the same “_id” as an existing record. You can achieve this either by passing the same object you got from the “find” result (after you’ve modified it to what you want to save to the DB), or by copying the “_id” field of that object to the other object you’re passing to “save” (“toInsert” in your case).
Thanks Ofer, can you give me an example of what this would look like, maybe in the form of some sample code. I’ve literally been trying to get this to work for over a week and am not making any progress.
Many thanks, Chris
Something like:
currentUser.getEmail().then(email => {
wixData.query('testData').eq('email', email).find()
.then(result => {
let theRecord = result.items[0]; // The existing record from the DB, assuming there is one
theRecord.firstName = $w('#input0').value // Do whatever modifications you need
wixData.save("testData", theRecord);
});
});
Ofer… you sir are the MAN
did u get your answer? i also have same problem!