Problem with SPFx TypeScript json.value.map(k,i) & query via getuserbyid()
-
07-02-2021 - |
Question
Firstly I'll start with extra question:
I'm trying to get data by _api/web/getuserbyid()
API call in code.
Do You guys know why there is no option for filtering by query for specific data? I mean by:
_api/web/getuserbyid(6)/items?$select=Title
But know going to the subject of .map
(the code is shown below) I've got error:
Uncaught (in promise) TypeError: Cannot read property 'map' of undefined
interface
export interface ISponsorData{
Title: string;
UserPrincipalName: string;
}
declaration
private _sponsorData: ISponsorData[] = [];
f
private _getSponsorDetails(): Promise<any[]>{
var _sponsorURL = `https://my.sharepoint.com/sites/SC/_api/web/getuserbyid(6)`;
return this.context.spHttpClient.get(_sponsorURL,SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse): Promise<{value: ISponsorData[]}> => {
return response.json();
})
.then((json: {value: ISponsorData[]}) => {
console.log(JSON.stringify(json.value));
return this._sponsorData=json.value.map((k,i) =>({
Title: k.Title,
UserPrincipalName: k.UserPrincipalName
}
));
});
}
for now log below is set as undefined (no suprise here)
console.log(JSON.stringify(json.value));
When I'm printing to the console console.log(response.json());
, I've proper response from the API call:
[PromiseValue]]: Object
@odata.context: "https://MY.sharepoint.com/sites/SC/_api/$metadata#Users/$entity"
@odata.editLink: "Web/GetUserById(6)"
@odata.id: "https://MY.sharepoint.com/sites/SC/_api/Web/GetUserById(6)"
@odata.type: "#SP.User"
Email: "name@MY.onmicrosoft.com"
Expiration: ""
Id: 6
IsEmailAuthenticationGuestUser: false
IsHiddenInUI: false
IsShareByEmailGuestUser: false
IsSiteAdmin: true
LoginName: "i:0#.f|membership|name@MY.onmicrosoft.com"
PrincipalType: 1
Title: "Name LastName"
I've found on stackoverflow, that the error might be caused if values were null, but they are not. What possible is wrong with my approach?
I've used =json.value.map((v,i) =>({
before and it is working, and it was also for spHttpClient Call.
No correct solution
OTHER TIPS
GetUserById
does not return a collection (array), map
is for iterating. GetUserById returns just a object.
You access the properties with; json.value.Title
For using $select
you can do: https://my.sharepoint.com/sites/SC/_api/web/getuserbyid(6)?$select=Title,UserPrincipalName