I need to get the members’ second email address. Searching in the corvid API library indicates that ‘userinfo’ gives all user info. But I can’t get them using the following code. It gives the following error:
“TypeError: Cannot read property ‘getUser’ of undefined”
Where the code is wrong?
import wixData from 'wix-data';
import wixUsers from 'wix-users';
$w.onReady(function () {
//TODO: write your page related code here...
let user = wixUsers.currentUser
let userID = user.id
console.log(userID)
});
let user = wixUsers.currentUser
let userId = user.id
console.log(userId)
import wixUsers2 from 'wix-users-backend';
export function getUser(id) {
return wixUsers2.getUser(id);
}
let j = getUser(userId)
You have errors in your code:
Is your code all in one file? If so, you are mixing frontend (wix-users) and backend (wix-users-backend) code together in the same file.
Setting wixUsers2 from wix-users-backend in a frontend file will result wixUsers2 being undefined - which is exactly what the error message is telling you.
Refer to the Corvid documentation for details on how to code in Corvid. Refer to the wix-users-backend API for details on how to use the Wix Users feature in backend code.
Thank Yisrael. Still have the same problem.
Trying to digest the concept I used a simple function from this site, that is a simple multiplication function.
As you said, I separated the code into two files.
1- backend file (mp.jsw)
//backend mp.jsw file
export function multiply(a, b) {
let res = a*b;
console.log(res)
return a*b;
}
2- frontend file (page code)
import {multiply} from 'backend/mp';
export function button1_click(event) {
//Add your code for this event here:
let c = multiply(3, 4)
console.log(c)
return multiply()
}
But still can’t get the result. Why?
I found what is wrong with my code.
For those who are toddler, like me, and might have in the same problem, the followings might help.
As Yisrael said, and according to the documentation, to get the user info using the object ‘userinfo’ (see this link), first, we should get the user info in a backend file (I think it is for security reasons) and then call the function ‘getUser’, which inside the backend file, from the frontend file (wix’s page code panel). So the code you see in this link is about all which should be in the backend file. In other words, one should
1- create a backend file which contains the function ‘getUser’ (the code in this link)
2- callback the function ‘getUser’ inside the frontend file (Wix’s page code panel)
Multiply case:
//backend mp.jsw file
export function multiply(a, b) {
let res = a*b;
return res;
}
// frontend file (wix's page code panel)
import { multiply } from 'backend/mp';
export function button1_click(event) {
//Add your code for this event here:
return multiply(3, 4)
.then((ff) => {
console.log(ff)
});
}
My own case:
//backend getUserInfo.jsw file
import wixUsers from 'wix-users-backend';
export function getUser(id) {
return wixUsers.getUser(id);
}
// frontend file (wix's page code panel)
import { getUser } from 'backend/getUserInfo'
let user = wixUsers.currentUser
let userID = user.id
getUser(userID)
.then((mm) => {
console.log(mm)
})
To be honest, I still don’t get why we should declare ‘then’, which make codes to seem more scary and entangled.
Nice job of working it out. To understand why you need .then() , you’ll need to understand asynchronous programming and Promises . See the following for more information:
Your observation on using backend for security is spot-on! Here’s some more material you might be interested in:
Have fun and keep rockin’!