Data update / insert in collection does not work

Hi,

Update or insert records do not work on my live site (i.e. they are not stored in the collection), whereas they do on the preview. Matter of permission ? but I did try several : administrator, member… Of course the dataset is r/w. The record in the table row is well actualized when save button hit, but not in the collection. Did I miss something and not really save/insert the data in the live collection ? As the page is password-protected, is it necessary to test for user id in the code ? But how ? (only password is asked for in the page permissions in the development platform and on the live page)
I went through a lot of posts in the forum without finding a solution.
website page : https://www.collongebasketclub.net/developpement
Here is the code :

if (save_toggle === 0) {  // save mods
     $w('#dataset1').setFieldValue('dateHeure',dt_finale);
     $w('#dataset1').setFieldValue('heure',(tH + ':' + tM));
     $w('#dataset1').setFieldValue('equipe',$w('#input1').value);
     $w('#dataset1').setFieldValue('championnat',$w('#input2').value);
     $w('#dataset1').setFieldValue('batBattu',$w('#input3').value);
     $w('#dataset1').setFieldValue('advLieu',$w('#input4').value);
     $w('#dataset1').setFieldValue('score',$w('#input5').value);
     $w('#dataset1').save()
        .catch( (err) => {
 let errMsg = err;
        console.log(errMsg);
        } );
     }
 if (save_toggle === 1) {  // insert
 let toInsert = {
 'dateHeure':  dt_finale,
 'heure':   (tH + ':' + tM),
 'equipe': $w('#input1'),
 'championnat': $w('#input2'),
 'batBattu': $w('#input3'),
 'advLieu': $w('#input4'),
 'score': $w('#input5')
        ('#dataset1').insert(toInsert)
        .then( (results) => {
 let item = results;
            console.log(results);
            } )
        }
        .catch( (err) => {
 let errMsg = err;
        console.log(errMsg);
        } );
}   }

First of all, check the collection permissions. Maybe they are set to Admin Only.
Second, in the case where save_toggle === 1, you forgot to write the value property - for example, you wrote:

'equipe': $w(#input1),
//instead of
$w("#input1").value,
//and so on
  • your second toInsert object has an error (You forgot to close it).
  • the insert() function works with your collection, I don’t think it works with datasets (but maybe I’m wrong. I’m not used to work with datasets).

@jonatandor35 , thank you for the answer, but still stuck :frowning:
The permissions are presently set to “members”, does not work.
In fact the “toinsert” object is closed after the first “console.log”. But I did already try to close it after “‘score’: $w(‘#input5’)”, it doesn’t work any better. I tried also with .value after the “#input…”, but I get an error msg (“value is a member but used as a function”) after the last one (ok for the other ones !!!)

'score': $w('#input5').value 

Moreover,

('#dataset1').insert(toInsert)

gives a “Typeerror” error…

in fact there is no “insert” property for the datasets, only update.

@francoisjuigner

  1. You must close the object after its last property (in your case after “score”).
  2. as I said you’re using the insert wrong. Try:
    https://www.wix.com/corvid/reference/wix-data.html#insert
1 Like

@jonatandor35 at least this code doesn’t give any longer any error. But the item is not inserted in the collection.
Nor does the update code update data in the collection, it does only on the table / dataset
I checked the permissions again, they are set to “personalized use”, read : everyone, insert, update and delete : site members

@francoisjuigner You can post the amended code here.

@jonatandor35 here is the complete code for the insert/update button click.

export function update_new_click(event) {
 let dtPick = $w('#datePicker1').value;
 let tPick = $w('#timePicker1').value;
 var dtD = dtPick.getDate();
 var dtM = dtPick.getMonth(); // jan = 0
 var dtY = dtPick.getFullYear();
 var tH = tPick.substr(0,2);
 var tM = tPick.substr(3,2);
 var dt1 = new Date('2018-10-26');
 var dt2 = new Date('2019-3-28')
 if (dtPick > dt1 && dtPick < dt2) { // gestion heure été/hiver pour -1 ou -2h pour enregistrer
 var TH = tH-1;}
 else {
 var TH = tH-2;}
 var dt_finale = new Date(dtY,dtM,dtD,TH+2,tM);  // TH +2 pour repasser à l'heure locale
 if (save_toggle === 0) {  // save mods
     $w('#dataset1').setFieldValue('dateHeure',dt_finale);
     $w('#dataset1').setFieldValue('heure',(tH + ':' + tM));
     $w('#dataset1').setFieldValue('equipe',$w('#input1').value);
     $w('#dataset1').setFieldValue('championnat',$w('#input2').value);
     $w('#dataset1').setFieldValue('batBattu',$w('#input3').value);
     $w('#dataset1').setFieldValue('advLieu',$w('#input4').value);
     $w('#dataset1').setFieldValue('score',$w('#input5').value);
     $w('#dataset1').save()
        .catch( (err) => {
 let errMsg = err;
        console.log(errMsg);
        } );
     }
 if (save_toggle === 1) {  // insert
 let toInsert = {
 'dateHeure':  dt_finale,
 'heure':   (tH + ':' + tM),
 'equipe': $w('#input1').value,
 'championnat': $w('#input2').value,
 'batBattu': $w('#input3').value,
 'advLieu': $w('#input4').value,
 'score': $w('#input5').value};
       wixData.insert('match', toInsert)
        .then( (results) => {
 let item = results;
            console.log(results);
            } )
        .catch( (err) => {
 let errMsg = err;
        console.log(errMsg);
        } );
}   }

@francoisjuigner now the syntax looks better.

  1. Did you remember to import wixData ?
  2. Make sure the values you get from datePicker1 and timePicker1 are of type of Date. If they’re not, you’ll have to convert them to type Date before you use the date methods.

@jonatandor35
Yes, wix-data is imported at the beginning, in the general section of the code.
I spent hours sorting out the dates issues (conversion of the live page date datepicker + time timepicker into the date+time field of the data manager), but it works fine now on the preview, with adequate results in the data manager sandbox, but not stored in live data (I read it is normal for the preview). Let’s hope it does from the live page too !
I went again through the Wix reference for permissions and role, looks ok for the ones I described (“custom use”, “read” : everyone, “insert”, “update” and “delete” : site members. Dataset on read/write. The “update_new” button is linked to the dataset, but not the entry fields. Should they be ?
The page mechanism is as follows : a table connected to the dataset displays the collection content with some user selectable filters. Selecting a row displays the item data in the entry fields, where they can be changed. Alternatively, a “new item” button blanks the entry fields and allows for new item data entry. The “update_new” button performs either “update” or “insert” according to the situation (by the means of “save_toggle”).
Presently, with an “update” operation, the row in the table gets properly updated, but not the collection (would suggest a permission issue ? should it be “author member” for the update/delete ?). The “insert” operation does nothing.

just tested : connecting the fields to the dataset allows an “insert” to display the new item in the table, but does not insert it in the collection. Does not change things for “update”.
Forgot to mention that I am not giving an “_id” value for the “insert”, I read somewhere that Wix is adding one when absent.