pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
753 stars 305 forks source link

Not a valid JSON #2959

Closed MarioZagreb closed 5 months ago

MarioZagreb commented 5 months ago

Major Version

3.x

Minor Version Number

3.0.10

Target environment

SharePoint Framework

Additional environment details

This is simple code that I use for user search in my SPfx web part:

var inputTxt = $("#txtNewEmployee").val(); if (inputTxt == undefined) inputTxt = ""; console.log("inputTxt: " + inputTxt);

sp.profiles.clientPeoplePickerSearchUser({
  AllowEmailAddresses: true,
  AllowMultipleEntities: false,
  AllUrlZones: false,
  MaximumEntitySuggestions: 5,
  PrincipalSource: PrincipalSource.All,
  PrincipalType: PrincipalType.User,
  QueryString: "John"//inputTxt.toString()
})
  .then((items) => {  
    console.log(items);  
    }

  })
  .catch(console.error);

Expected or Desired Behavior

I use this code to find users and set them to a custom form field.

Observed Behavior

Everything was working fine until recently. Now, I just get this error and I don't know why:

SyntaxError: "[object Object]" is not valid JSON at JSON.parse ()

Steps to Reproduce

This line is written to console: console.log("inputTxt: " + inputTxt);

but this line is not: console.log(items);

bcameron1231 commented 5 months ago

Hi. Unfortunately, I am unable to reproduce the issue. Do you think you could open up the network tab in Dev Tools (F12) to show us the response coming back from the request? I would grab that response and paste it into a JSON validator. It would help rule out if the issue is happening within the library, or coming from SharePoint

MarioZagreb commented 5 months ago

{ "d": { "ClientPeoplePickerSearchUser": "[{\"Key\":\"i:0#.f|membership|mario.mySurname@aaa.net\",\"DisplayText\":\"mySurname Mario\",\"IsResolved\":true,\"Description\":\"mario.mySurname@aaa.net\",\"EntityType\":\"User\",\"EntityData\":{\"IsAltSecIdPresent\":\"False\",\"UserKey\":\"i:0h.f|membership|100@live.com\",\"Title\":\"ENGINEER\",\"Email\":\"mario.mySurname@aaa.hr\",\"MobilePhone\":\"+0000000000009\",\"ObjectId\":\"988f6d04-55b6-4a9c-93b7-70cc64f7ae81\",\"Department\":\"aaa ICT\"},\"MultipleMatches\":[],\"ProviderName\":\"Tenant\",\"ProviderDisplayName\":\"Tenant\"},{\"Key\":\"i:0#.f|membership|srdan.mySurnameic@aaa.net\",\"DisplayText\":\"mySurnameIC Srdan\",\"IsResolved\":true,\"Description\":\"mario.mySurnameic@aaa.net\",\"EntityType\":\"User\",\"EntityData\":{\"IsAltSecIdPresent\":\"False\",\"UserKey\":\"i:0h.f|membership|10@live.com\",\"Title\":\"ENGINEER\",\"Email\":\"mario.mySurnameic@aaa.hr\",\"MobilePhone\":\"+385 00000000002\",\"ObjectId\":\"bf69ee28-bb0b-492a-a756-415d965e8822\",\"Department\":\"aaa ICT\"},\"MultipleMatches\":[],\"ProviderName\":\"Tenant\",\"ProviderDisplayName\":\"Tenant\"}]" } }

bcameron1231 commented 5 months ago

Hi. This does appear to be invalid JSON. It's not escaped, so it's failing at "[{"

To confirm, this is the response found in the Web Browser in Dev Tools from the SharePoint API?

MarioZagreb commented 5 months ago

Hi, yes it is a response from the SP API in web browser dev tools. Can you share valid JSON that I am supposed to get?

bcameron1231 commented 5 months ago

Hi, so looking into this more.... do you think you could share how you're initiating your SPFI object? Are you using any behaviors (Parsers or Headers)?

MarioZagreb commented 5 months ago

Hi, here it is:

pnp.setup({ sp: { baseUrl: "https://aaa", headers: { "Accept": "application/json; odata=verbose" } } });

You are right, I am using headers and I remember adding the line with headers recently, but I can't remember why :)

bcameron1231 commented 5 months ago

If you remove those custom headers, does it work?

MarioZagreb commented 5 months ago

Yes, it does work without headers

MarioZagreb commented 5 months ago

I there a way for initiating SPFI object without using baseUrl? pnp.setup({ sp: { baseUrl: "https://aaa" } });

Can it be done automatically?

patrick-rodgers commented 5 months ago

pnp.setup is not found in v3 - what version are you really using?

MarioZagreb commented 5 months ago

This is from my package.json: "sp-pnp-js": "^3.0.10"

juliemturner commented 5 months ago

So you're not using a current version of the library. The one you're referencing has been deprecated, as you can see, about 6 years ago: image

please see the documentation here for updating to the latest library: PnPjs Getting Started - Specifically the install section. There is also some example projects there that you can reference.

patrick-rodgers commented 5 months ago

Closing this issue as answered. If you have additional questions or we did not answer your question, please open a new issue, ref this issue, and provide any additional details available. Thank you!

github-actions[bot] commented 5 months ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.