I am new to SPFx and wrapping my head around getting the current logged in user on SPFx using React , I did refer to a few posts here , However couldn't find the resolution I've been looking for.
My end goal is to get the current logged in userID and store the value in a variable to be used in RestAPI later
Below is the code sample
public componentdidmount() {
var u;
sp.web.currentUser.get().then(
(user) => {
console.log(user);
u = user.UserId;
},
(errorResponse) => {
debugger;
console.log(errorResponse);
}
); //Get Current User*/
sp.web.lists
.getByTitle("SampleList")
.items.select(
"*",
"CourseName/Title",
"Attendee/Title",
"Attendee/ID",
"Status/Title",
"Completed",
"AttachmentFiles"
)
.expand("CourseName", "Attendee", "Status", "AttachmentFiles")
.filter("Status/Title eq 'Not Started' and Attendee eq '" + u + "'")
.top(4999)
.orderBy("Created", false)
.get()
.then((response) => {
let getdetails = response.map((item) => new EntityListItems(item));
});
}
While debugging , I get an undefined value on the variable u.
I would be grateful if someone could help me with storing the current logged in userID to a variable so that I can pass the value to the Rest call.
Any help would be appreciated.