pnpjs icon indicating copy to clipboard operation
pnpjs copied to clipboard

Error fetching page details

Open raymondlittle100 opened this issue 8 years ago • 26 comments

Category

  • [ ] Enhancement
  • [x] Bug
  • [ ] Question
  • [ ] Documentation gap/issue

Version

Please specify what version of the library you are using: [ 1.1.0 ]

Expected / Desired Behavior / Question

Able to load page details by page Url

Observed Behavior

Getting JSON token errors, see below

"SyntaxError: Unexpected token . in JSON at position 343" "stack:"SyntaxError: Unexpected token . in JSON at position 343\n at JSON.parse ()\n at Function.ClientSidePage.escapedStringToJson (https://localhost:4321/dist/pn-p-web-part.js:5897:21)\n at ClientSideWebpart.fromHtml (https://localhost:4321/dist/pn-p-web-part.js:6385:42)\n at https://localhost:4321/dist/pn-p-web-part.js:5951:29\n at getBoundedDivMarkup (https://localhost:4321/dist/pn-p-web-part.js:5780:29)\n at ClientSidePage.fromHtml (https://localhost:4321/dist/pn-p-web-part.js:5935:9)\n at https://localhost:4321/dist/pn-p-web-part.js:5972:19""

Steps to Reproduce

Created a basic web part for testing which setups PnP context and passes this to react component

public onInit(): Promise {

return super.onInit().then(_ => {

  // other init code may be present

  sp.setup({
    spfxContext: this.context
  });
});

}

In react component try and fetch a page. If there is any content on the page like text web part of links web part or a custom web part it errors. If the blank page it works ok.

const page = await ClientSidePage.fromFile( this.props.spRest.web.getFileByServerRelativeUrl(this.props.pageUrl));

raymondlittle100 avatar Jun 11 '18 14:06 raymondlittle100

Does this work correctly outside of the react application? I am not able to duplicate making the plain call.

patrick-rodgers avatar Jun 11 '18 15:06 patrick-rodgers

I never got this in 1.04 or 1.0.5 it only seems to be 1.1.1

Tried adding this directly in the web part ts file and got same issue

image

SyntaxError: Unexpected token [ in JSON at position 107 at JSON.parse () at Function.ClientSidePage.escapedStringToJson (https://localhost:4321/dist/pn-p-web-part.js:6101:21) at ClientSideWebpart.fromHtml (https://localhost:4321/dist/pn-p-web-part.js:6589:42) at https://localhost:4321/dist/pn-p-web-part.js:6155:29 at getBoundedDivMarkup (https://localhost:4321/dist/pn-p-web-part.js:5984:29) at ClientSidePage.fromHtml (https://localhost:4321/dist/pn-p-web-part.js:6139:9) at https://localhost:4321/dist/pn-p-web-part.js:6176:19

raymondlittle100 avatar Jun 11 '18 15:06 raymondlittle100

So far as I know none of our code has changed between those releases that would affect this scenario. Will try and duplicate.

patrick-rodgers avatar Jun 11 '18 17:06 patrick-rodgers

Happy to share any more details you need

raymondlittle100 avatar Jun 11 '18 19:06 raymondlittle100

I can't duplicate this exactly - what all is on the page? I did find an issue related to text web parts sticking in an extra

due to how we were parsing this, I think related but not solving your other issue #126.

patrick-rodgers avatar Jun 12 '18 14:06 patrick-rodgers

Can you capture the actual content the JSON parser is failing on?

patrick-rodgers avatar Jun 12 '18 15:06 patrick-rodgers

I'll get a sample of the page, it mainly seemed to be when any content, as in any web part, was on the page. Blank pages were fine. What would be the best way to get the JSON? Look at maintenance mode?

raymondlittle100 avatar Jun 12 '18 19:06 raymondlittle100

Tried the following but it gives me the error below when trying to getJSON

Sample code console.log('test 1'); const file = await this.props.spRest.web.getFileByServerRelativeUrl(this.props.pageUrl); console.log('test 2'); const fileJson = await file.getJSON(); console.log('test 3'); const page = await ClientSidePage.fromFile(file);

Result SyntaxError: Unexpected token < in JSON at position 0

Screenshot of web parts_ image

Screenshot of maintenancemode image

In this example I have

  • Custom web part

  • Link web part

  • Image web part

  • Text web part

raymondlittle100 avatar Jun 12 '18 20:06 raymondlittle100

Oh - I think you don't need the getJSON step...the file won't be in JSON, so that might be the source of your error. Can you try passing the file object directly to the fromFile method, as shown here.

import { 
    sp,
    ClientSidePage,
} from "@pnp/sp";

// the fromFile just needs the file instance, ClientSidePage will handle all the loading from there.
const page = await ClientSidePage.fromFile(sp.web.getFileByServerRelativeUrl("/sites/dev/SitePages/ExistingFile.aspx"));

patrick-rodgers avatar Jun 12 '18 21:06 patrick-rodgers

Thats what I originally had. If i try this code I get the error below.

Just thinking, depending what the PnP code does, it could be due to our custom web parts. We have multiple versions of the same web part for different environments in the same tenant. To achieve this our web parts have environment settings per build and the web parts are environment prefixed e.g. [development]Page Information, [Integration] Page information. Could this be where the '['in the error comes from?

Code const page = await ClientSidePage.fromFile( this.props.spRest.web.getFileByServerRelativeUrl(this.props.pageUrl));

Error Message Unexpected token [ in JSON at position 276

Stack trace TypeError: Cannot read property 'responseBody' of undefined PnPPageDetails.tsx:96 at PnPPageDetails. (d:\RayGIT\RL-Test-SPFx-WebParts\PnPTesting\src\webparts\pnP\components\PnPPageDetails.tsx:96:32) at step (d:\RayGIT\RL-Test-SPFx-WebParts\PnPTesting\dist\pn-p-web-part.js:10887:24) at Object.throw (d:\RayGIT\RL-Test-SPFx-WebParts\PnPTesting\dist\pn-p-web-part.js:10868:54) at rejected (d:\RayGIT\RL-Test-SPFx-WebParts\PnPTesting\dist\pn-p-web-part.js:10860:66)

raymondlittle100 avatar Jun 13 '18 07:06 raymondlittle100

@raymondlittle100 I am also facing the same issue. i downgraded @pnp/sp to "@pnp/sp": "^1.0.4" Now it works

harshdamaniahd avatar Jun 13 '18 09:06 harshdamaniahd

Are you both only upgrading a single package? All packages need to be upgraded to latest version for things to work. You would have gotten errors in the command line about missing peer dependencies.

patrick-rodgers avatar Jun 13 '18 14:06 patrick-rodgers

No I have updated all

image

raymondlittle100 avatar Jun 13 '18 14:06 raymondlittle100

@raymondlittle100 after upgrading all packages to 1.1.1 and still it doesn't work right ?

harshdamaniahd avatar Jun 13 '18 14:06 harshdamaniahd

@raymondlittle100 or @harshdamaniahd can either of you share the content of the JSON that is failing to parse? Or share the code around the call? Without seeing the actual data I can only guess what might be happening.

patrick-rodgers avatar Jun 13 '18 14:06 patrick-rodgers

@patrick-rodgers happy to help if you let me know the best way to get the JSON

raymondlittle100 avatar Jun 13 '18 14:06 raymondlittle100

You can set a breakpoint in that method where it throws the error and get the value then. My problem currently is that knowing there is a JSON parsing error isn't a thing I can fix since I can't recreate whatever is happening. The other option is to provide steps that will recreate the issue. Something like add this web part with these settings, add this web part with these settings, add a text web part with exactly this content, etc.

Or you can wait until Friday when we publish the beta that will have the fix for the text content parsing from #126. Perhaps that will help here and these pages have gotten corrupted.

patrick-rodgers avatar Jun 13 '18 18:06 patrick-rodgers

Page with hero web part gets error "SyntaxError: Unexpected token [ in JSON at position 276" with JSON, i think,

{"controlType":3,"displayMode":2,"id":"66d4cd07-0391-4650-81db-cf04cde17fc0","position":{"zoneIndex":1,"sectionIndex":1,"controlIndex":1},"webPartId":"c4bd7b2f-7b6e-4599-8485-16504575f590"} clientsidepages.ts:267 {"id":"c4bd7b2f-7b6e-4599-8485-16504575f590","instanceId":"66d4cd07-0391-4650-81db-cf04cde17fc0","title":"Hero","description":"Prominently display up to 5 pieces of content in a highly visual layout","serverProcessedContent":{"htmlStrings":{},"searchablePlainTexts":{"content[0].title":"","content[1].title":"","content[2].title":"","content[3].title":"","content[4].title":"","content[0].alternateText":"","content[1].alternateText":"","content[2].alternateText":"","content[3].alternateText":"","content[4].alternateText":""},"imageSources":{},"links":{},"componentDependencies":{"heroLayoutComponentId":"9586b262-54de-4b27-9eb9-34c671400c33","carouselLayoutComponentId":"8ac0c53c-e8d0-4e3e-87d0-7449eb0d4027"},"customMetadata":{"heroLayoutComponentId":{"minCanvasWidth":640},"carouselLayoutComponentId":{"maxCanvasWidth":639}}},"dataVersion":"1.3","properties":{"heroLayoutThreshold":640,"carouselLayoutMaxWidth":639,"layoutCategory":1,"layout":5,"content":[{"id":"36f4977f-688d-4fa2-827f-ec4c3a7c6290","type":"Image","color":4,"description":"","showDescription":false,"showTitle":true,"imageDisplayOption":0,"isDefaultImage":false,"showCallToAction":false,"isDefaultImageLoaded":false,"isCustomImageLoaded":false,"showFeatureText":false},{"id":"2b66c8a5-106e-4066-a27c-7569ee71c981","type":"Image","color":4,"description":"","showDescription":false,"showTitle":true,"imageDisplayOption":0,"isDefaultImage":false,"showCallToAction":false,"isDefaultImageLoaded":false,"isCustomImageLoaded":false,"showFeatureText":false},{"id":"56372181-93ad-48b9-b9c9-e21a12c09f91","type":"Image","color":4,"description":"","showDescription":false,"showTitle":true,"imageDisplayOption":0,"isDefaultImage":false,"showCallToAction":false,"isDefaultImageLoaded":false,"isCustomImageLoaded":false,"showFeatureText":false},{"id":"25c91620-fd9d-4e4c-b48c-95ac29f4251c","type":"Image","color":4,"description":"","showDescription":false,"showTitle":true,"imageDisplayOption":0,"isDefaultImage":false,"showCallToAction":false,"isDefaultImageLoaded":false,"isCustomImageLoaded":false,"showFeatureText":false},{"id":"ad63c722-b208-4e23-940e-f3489373cd66","type":"Image","color":4,"description":"","showDescription":false,"showTitle":true,"imageDisplayOption":0,"isDefaultImage":false,"showCallToAction":false,"isDefaultImageLoaded":false,"isCustomImageLoaded":false,"showFeatureText":false&#

Same page with no web parts, which works.

{"displayMode":2,"position":{"sectionIndex":1,"zoneIndex":1}} clientsidepages.ts:267 {"displayMode":2,"position":{"sectionIndex":1,"zoneIndex":1}}

Same page with custom web part with same error {"controlType":3,"displayMode":2,"id":"c192fb7e-3398-4e21-a519-511636743f1e","position":{"zoneIndex":1,"sectionIndex":1,"controlIndex":1},"webPartId":"e4cbbb07-342e-481f-9521-8025d426e73a"} clientsidepages.ts:267 {"id":"e4cbbb07-342e-481f-9521-8025d426e73a","instanceId":"c192fb7e-3398-4e21-a519-511636743f1e","title":"[development]PageInformation","description":"Web part to show current page metadata","serverProcessedContent":{"htmlStrings":{},"searchablePlainTexts":{},"imageSources":{},"links":{}},"dataVersion":"1.0","properties":{"description":"PageInformation"}}

raymondlittle100 avatar Jun 14 '18 08:06 raymondlittle100

Thanks, I can now recreate this with the hero part so let me see what's happening.

patrick-rodgers avatar Jun 14 '18 12:06 patrick-rodgers

It looks like there was a chance on the server to escape the '[', ']', and '.' chars in the string, so it couldn't decode correctly. Added code to handle this and as well return the server processed content to the server. PR will be merged pending CI checks. Thanks for helping out with the details, this will be part of Friday's beta release.

patrick-rodgers avatar Jun 14 '18 14:06 patrick-rodgers