How to filter the retrieved items using a pnp-js-core REST call to a SharePoint list
Question
As I am new to Sharepoint framework , I am trying to filter the retrieved list items from sharepoint list. Please find below code:
pnp.sp.web.currentUser.get().then(function(res){
var userEmail = res.Email;
});
pnp.sp.web.lists.getByTitle('EmployeeMaster').items.select("EmployeeID,Title,Designation,Email").filter("Email eq " + userEmail + "'").get().then(function(results){
var empID = results[0].EmployeeID;});
Above code is giving me an error of undefined. Please suggest an workaround...
Solution
You could check the code for this error(SitePages).
If it is caused by this error, you can find the solution here.
https://github.com/SharePoint/PnP-JS-Core/wiki/Using-sp-pnp-js-in-SharePoint-Framework
Updated code:
pnp.sp.web.currentUser.get().then(function(res){
var userEmail = res.Email;
pnp.sp.web.lists.getByTitle('EmployeeMaster').items.select("EmployeeID,Title,Designation,Email").filter("Email eq '" + userEmail + "'").get().then(function(results){
var empID = results[0].EmployeeID;});
});
js asynchronous request order solution for your reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
OTHER TIPS
It looks like you're missing a single quote in your filter expression.
filter("Email eq " + userEmail + "'")
should be
filter("Email eq '" + userEmail + "'")