I have a field in a database to contain the email address of the logged in user making a change.
It seems to only work sometimes?
Here is my code… according to the API reference.
import wixUsers from ‘wix-users’ ;
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;
import {session} from ‘wix-storage’ ;
let userEmail;
$w.onReady( function () {
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn; // true
user.getEmail()
.then( (email) => {
userEmail = email; // “user@something.com”
} );
let formType = session.getItem( “Edit#Sel” );
let dashNo = session.getItem( “dashNo” );
console.log( “Got Dash#” + dashNo)
if (formType === “Edit” ){
$w( “#dataset1” ).setFilter(wixData.filter()
.eq( “dash” , dashNo)
)
session.clear();
} else {
$w( “#button2” ).label = “Submit” ;
}
// after the user clicks the Submit button update the updatedBy field
$w( “#dataset1” ).onAfterSave( function () {
console.log( "Updated by: " , userEmail);
$w( ‘#dataset1’ ).setFieldValue( “updatedBy” , userEmail);
$w( “#dataset1” ).save();
});
});
PLEASE HELP…
Thanks
JD
Hey JD!
Care to share a URL to your site so we can check that out?
Doron.
cosworthvega.com
database: cvoa-registry
page: register-new-car
it is a members only page. let me know if you need the wix@test.com login credentials.
Thanks
JD
Java Console Shows Error:
wixID = 11f2faac-e158-41ff-a9f4-295b89439fd6 / Email = cwvega76@gmail.com
console.js:35 Updated by: cwvega76@gmail.com
bolt-worker.js:1 Uncaught (in promise) r {code: “OPERATION_NOT_ALLOWED”, message: “Operation (save) not allowed during save”, name: “DatasetError”, stack: “DatasetError: Operation (save) not allowed during …ervices/dbsm-viewer-app/1.1262.0/app.js:6:177002)”, Symbol(error-boundary-scope): “userCodeZone”, …}
Any ideas?
Response by Steve Cropper on Totally Codable Facebook page… as no one here has responded…
JD Smith it’s somewhat subtle but you are creating an infinite loop.
When you first save the dataset your onAfterSave event handler is called. Then in the middle of the event handler executing you call save() again. Guess what? This then calls your onAfterSave handler which then calls your save function which again triggers your onAfterSave handler.
Bottom line never call a function that has an event handler inside of the functions event handler
JD Smith Steve Cropper You are exactly right! Rookie error!
I HAVE RECODED THE PAGE AS FOLLOWS:
import wixUsers from ‘wix-users’ ;
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;
import {session} from ‘wix-storage’ ;
let userEmail;
$w.onReady( function () {
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn; // true
user.getEmail()
.then( (email) => {
userEmail = email; // “user@something.com”
} );
let formType = session.getItem( “Edit#Sel” );
let dashNo = session.getItem( “dashNo” );
console.log( “Got Dash#” + dashNo)
if (formType === “Edit” ){
$w( “#dataset1” ).setFilter(wixData.filter()
.eq( “dash” , dashNo)
)
session.clear();
} else {
$w( “#button2” ).label = “Submit” ;
}
});
async function update(updatedBy) {
$w( “#dataset1” ).onAfterSave( function () {
console.log( "Updated by: " , userEmail);
$w( ‘#dataset1’ ).setFieldValue( “updatedBy” , userEmail);
$w( “#dataset1” ).save();
});
}
IT WORKS ON RECORD EMAIL MATCHES LOGGED IN USER. HOWEVER, FOR OTHER RECORDS IT DOES NOT WORK…?
Problem resolved using another approach.