JSReports:意外的令牌<在位置0的JSON中

发布于 2025-02-13 17:49:31 字数 6587 浏览 1 评论 0原文

这可能需要熟悉JSReports的人,但是欢迎其他人的想法...

我知道“在位置0处的JSON中意外的令牌”通常表明将HTML格式化的字符串发送到JSON.PARSE(),所以我已经进行了调试,以确保我认为我正在进入JSReports渲染引擎的对象是有效的JSON,但我不确定这就是它所困扰的。

脚本文件(content.js):

// server side script fetching remote data and preparing report data source
const request = require('request');

// call remote http rest api
async function fetchCircleIn() {
    return new Promise((resolve, reject) => {
        const options = {
        url: 'http://localhost:5000/API/Meets/14/Events?ForCircleIn=true',
        method: 'GET',
        json:true };

        request(options,
        function(error, response, body) {
            if(error){
                console.log(error);
                reject(error);
            }
            else{
                console.log(body);
                resolve(body);
            }
        });
    })
}

// add jsreport hook which modifies the report input data
async function beforeRender(req, res) {
    let data = await fetchCircleIn();
    console.log(data);
    req.data.Individuals = data;
}

这是该响应的删节版本:

[
{
    "IndividualId": 6,
    "FirstName": "Jeff",
    "NickName": null,
    "LastName": "Moore",
    "Age": 35,
    "IsTeamCaptain": false,
    "Events": [
        {
            "EventId": 18,
            "EventDescription": "Progressive Freestyle Relay",
            "EventNumber": "5",
            "DistanceNum": 250,
            "ScheduleTime": "10:00:00-05",
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:18:06.591Z",
            "MeetId": 14
        },
        {
            "EventId": 11,
            "EventDescription": "Watermelon Relay Race",
            "EventNumber": "8",
            "DistanceNum": 100,
            "ScheduleTime": "11:00:00-05",
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:10:53.180Z",
            "MeetId": 14
        },
        {
            "EventId": 22,
            "EventDescription": "Decades Relay Race",
            "EventNumber": "13",
            "DistanceNum": 25,
            "ScheduleTime": null,
            "AgeMin": 21,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:20:30.427Z",
            "MeetId": 14
        }
    ]
},
{
    "IndividualId": 1,
    "FirstName": "Mark",
    "NickName": null,
    "LastName": "Rodenhauser",
    "Age": 41,
    "IsTeamCaptain": null,
    "Events": [
        {
            "EventId": 10,
            "EventDescription": "Corkscrew Individual Race",
            "EventNumber": "6",
            "DistanceNum": 25,
            "ScheduleTime": "10:30:00-05",
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:09:47.269Z",
            "MeetId": 14
        },
        {
            "EventId": 1,
            "EventDescription": "Doggie Paddle",
            "EventNumber": "11",
            "DistanceNum": 25,
            "ScheduleTime": null,
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2022-06-28T03:31:55.892Z",
            "MeetId": 14
        },
        {
            "EventId": 14,
            "EventDescription": "Dragon Fly Individual Race",
            "EventNumber": "12",
            "DistanceNum": 25,
            "ScheduleTime": null,
            "AgeMin": 21,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:14:37.616Z",
            "MeetId": 14
        }
    ]
},
{
    "IndividualId": 2,
    "FirstName": "Britt",
    "NickName": null,
    "LastName": "Sensat",
    "Age": 43,
    "IsTeamCaptain": true,
    "Events": [
        {
            "EventId": 18,
            "EventDescription": "Progressive Freestyle Relay",
            "EventNumber": "5",
            "DistanceNum": 250,
            "ScheduleTime": "10:00:00-05",
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:18:06.591Z",
            "MeetId": 14
        },
        {
            "EventId": 11,
            "EventDescription": "Watermelon Relay Race",
            "EventNumber": "8",
            "DistanceNum": 100,
            "ScheduleTime": "11:00:00-05",
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:10:53.180Z",
            "MeetId": 14
        },
        {
            "EventId": 1,
            "EventDescription": "Doggie Paddle",
            "EventNumber": "11",
            "DistanceNum": 25,
            "ScheduleTime": null,
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2022-06-28T03:31:55.892Z",
            "MeetId": 14
        },
        {
            "EventId": 22,
            "EventDescription": "Decades Relay Race",
            "EventNumber": "13",
            "DistanceNum": 25,
            "ScheduleTime": null,
            "AgeMin": 21,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:20:30.427Z",
            "MeetId": 14
        }
    ]
}
]

在本地运行JSReports的控制台输出:

*Waiting for the debugger to disconnect...
2022-07-07T13:21:41.411Z - debug: Replaced assets ["circle-in-styles.css"]
2022-07-07T13:21:41.413Z - debug: Rendering engine handlebars using dedicated-process strategy
Debugger listening on ws://127.0.0.1:52364/cde5a262-592c-4ca2-abaf-90582f0ffd4b
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for2022-07-07T13:21:41.699Z - warn: Rendering request 2 finished with error in 2306 ms
 the 2022-07-07T13:21:41.700Z - warn: Error when processing render request 2 Unexpected token < in JSON at position 0 SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at module.exports (C:\Users\mark.rodenhauser\Repos\GitHub\Lakewood-Lightning-Funmeet-Reporting\node_modules\jsreport-core\lib\render\resolveReferences.js:4:17)
    at module.exports (C:\Users\mark.rodenhauser\Repos\GitHub\Lakewood-Lightning-Funmeet-Reporting\node_modules\jsreport-core\lib\render\engineScript.js:35:17)
    at process.<anonymous> (C:\Users\mark.rodenhauser\Repos\GitHub\Lakewood-Lightning-Funmeet-Reporting\node_modules\script-manager\lib\worker-processes.js:66:36)
    at process.emit (node:events:520:28)
    at emit (node:internal/child_process:938:14)
    at processTicksAndRejections (node:internal/process/task_queues:84:21)
debugger to disconnect...
2022-07-07T13:21:41.702Z - warn: Error during processing request at http://localhost:5488/api/report/Circle-In
*

This may require someone familiar with JSReports, but ideas from others are welcome...

I know the "unexpected token < in JSON at position 0" is typically an indication of an HTML formatted string being sent to JSON.parse(), so I've done debugging to ensure the object that I THINK I'm passing into the JSReports rendering engine is valid JSON, but I'm not sure that's what it's getting stuck on.

Script file (content.js):

// server side script fetching remote data and preparing report data source
const request = require('request');

// call remote http rest api
async function fetchCircleIn() {
    return new Promise((resolve, reject) => {
        const options = {
        url: 'http://localhost:5000/API/Meets/14/Events?ForCircleIn=true',
        method: 'GET',
        json:true };

        request(options,
        function(error, response, body) {
            if(error){
                console.log(error);
                reject(error);
            }
            else{
                console.log(body);
                resolve(body);
            }
        });
    })
}

// add jsreport hook which modifies the report input data
async function beforeRender(req, res) {
    let data = await fetchCircleIn();
    console.log(data);
    req.data.Individuals = data;
}

Here's an abridged version of that response:

[
{
    "IndividualId": 6,
    "FirstName": "Jeff",
    "NickName": null,
    "LastName": "Moore",
    "Age": 35,
    "IsTeamCaptain": false,
    "Events": [
        {
            "EventId": 18,
            "EventDescription": "Progressive Freestyle Relay",
            "EventNumber": "5",
            "DistanceNum": 250,
            "ScheduleTime": "10:00:00-05",
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:18:06.591Z",
            "MeetId": 14
        },
        {
            "EventId": 11,
            "EventDescription": "Watermelon Relay Race",
            "EventNumber": "8",
            "DistanceNum": 100,
            "ScheduleTime": "11:00:00-05",
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:10:53.180Z",
            "MeetId": 14
        },
        {
            "EventId": 22,
            "EventDescription": "Decades Relay Race",
            "EventNumber": "13",
            "DistanceNum": 25,
            "ScheduleTime": null,
            "AgeMin": 21,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:20:30.427Z",
            "MeetId": 14
        }
    ]
},
{
    "IndividualId": 1,
    "FirstName": "Mark",
    "NickName": null,
    "LastName": "Rodenhauser",
    "Age": 41,
    "IsTeamCaptain": null,
    "Events": [
        {
            "EventId": 10,
            "EventDescription": "Corkscrew Individual Race",
            "EventNumber": "6",
            "DistanceNum": 25,
            "ScheduleTime": "10:30:00-05",
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:09:47.269Z",
            "MeetId": 14
        },
        {
            "EventId": 1,
            "EventDescription": "Doggie Paddle",
            "EventNumber": "11",
            "DistanceNum": 25,
            "ScheduleTime": null,
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2022-06-28T03:31:55.892Z",
            "MeetId": 14
        },
        {
            "EventId": 14,
            "EventDescription": "Dragon Fly Individual Race",
            "EventNumber": "12",
            "DistanceNum": 25,
            "ScheduleTime": null,
            "AgeMin": 21,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:14:37.616Z",
            "MeetId": 14
        }
    ]
},
{
    "IndividualId": 2,
    "FirstName": "Britt",
    "NickName": null,
    "LastName": "Sensat",
    "Age": 43,
    "IsTeamCaptain": true,
    "Events": [
        {
            "EventId": 18,
            "EventDescription": "Progressive Freestyle Relay",
            "EventNumber": "5",
            "DistanceNum": 250,
            "ScheduleTime": "10:00:00-05",
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:18:06.591Z",
            "MeetId": 14
        },
        {
            "EventId": 11,
            "EventDescription": "Watermelon Relay Race",
            "EventNumber": "8",
            "DistanceNum": 100,
            "ScheduleTime": "11:00:00-05",
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:10:53.180Z",
            "MeetId": 14
        },
        {
            "EventId": 1,
            "EventDescription": "Doggie Paddle",
            "EventNumber": "11",
            "DistanceNum": 25,
            "ScheduleTime": null,
            "AgeMin": 0,
            "AgeMax": 99,
            "CreatedDtm": "2022-06-28T03:31:55.892Z",
            "MeetId": 14
        },
        {
            "EventId": 22,
            "EventDescription": "Decades Relay Race",
            "EventNumber": "13",
            "DistanceNum": 25,
            "ScheduleTime": null,
            "AgeMin": 21,
            "AgeMax": 99,
            "CreatedDtm": "2021-06-02T03:20:30.427Z",
            "MeetId": 14
        }
    ]
}
]

Console output running JSReports locally:

*Waiting for the debugger to disconnect...
2022-07-07T13:21:41.411Z - debug: Replaced assets ["circle-in-styles.css"]
2022-07-07T13:21:41.413Z - debug: Rendering engine handlebars using dedicated-process strategy
Debugger listening on ws://127.0.0.1:52364/cde5a262-592c-4ca2-abaf-90582f0ffd4b
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for2022-07-07T13:21:41.699Z - warn: Rendering request 2 finished with error in 2306 ms
 the 2022-07-07T13:21:41.700Z - warn: Error when processing render request 2 Unexpected token < in JSON at position 0 SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at module.exports (C:\Users\mark.rodenhauser\Repos\GitHub\Lakewood-Lightning-Funmeet-Reporting\node_modules\jsreport-core\lib\render\resolveReferences.js:4:17)
    at module.exports (C:\Users\mark.rodenhauser\Repos\GitHub\Lakewood-Lightning-Funmeet-Reporting\node_modules\jsreport-core\lib\render\engineScript.js:35:17)
    at process.<anonymous> (C:\Users\mark.rodenhauser\Repos\GitHub\Lakewood-Lightning-Funmeet-Reporting\node_modules\script-manager\lib\worker-processes.js:66:36)
    at process.emit (node:events:520:28)
    at emit (node:internal/child_process:938:14)
    at processTicksAndRejections (node:internal/process/task_queues:84:21)
debugger to disconnect...
2022-07-07T13:21:41.702Z - warn: Error during processing request at http://localhost:5488/api/report/Circle-In
*

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁绊已千年 2025-02-20 17:49:31

谢谢,我要为Hobby/Hobby/志愿者/慈善机构所做的所有项目选择,一年后,事情就没有工作,所以我弄清楚发生了什么变化。 @quentin是正确的,我应该已经查看了“网络”选项卡,因为这样做已经强调了问题完全与我的API响应无关。 JSReports正在遇到我相信的模板的问题……如果您问我,产品在产品中处理的一个可怕的错误。我将关闭这一点,获取最新的JSReports,并重新实现(2)报告,并从“请求”软件包中重新分配。谢谢! -
g

Thanks all I'm picking up this project I'm doing for hobby/volunteer/charity after a year off and stuff's just not working as it was so I'm figuring out what's changed. @Quentin is right that I should have already looked at the network tab, because doing so has highlighted that the issue is not related to my API response at all; JSReports is having issues looking up the template I believe...just a terrible error handling in the product if you ask me. I'm going to close this, get latest JSReports, and re-implement the (2) reports, as well as refactor away from 'request' package. thanks! –
G

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文