pengzhanbo / vite-plugin-mock-dev-server

🚀mock-dev-server is injected into the vite development environment to simulate requests and data responses.在vite 开发环境中注入 mock-dev-server, 模拟请求和数据响应
https://vite-plugin-mock-dev-server.netlify.app
MIT License
169 stars 14 forks source link

body内容某些情况下存在JSON解析失败 #96

Closed itmanyong closed 2 months ago

itmanyong commented 2 months ago

Bug 描述

系统环境

- OS:win10 pro x64
- Node:20.16.0
- packageManager:pnpm 9.7.1
- vite:5.4.2
- vite-plugin-mock-dev-server:^1.7.1

Logs

SyntaxError: Expected double-quoted property name in JSON at position 485 at JSON.parse () at exports.parse (D:\Administrator\MyDocuments\GitHub\vite-vue-templte-js\node_modules.pnpm\@hapi+bourne@3.0.0\node_modules\@hapi\bourne\lib\index.js:19:22) at parse (D:\Administrator\MyDocuments\GitHub\vite-vue-templte-js\node_modules.pnpm\co-body@6.2.0\node_modules\co-body\lib\json.js:59:19) at module.exports [as json] (D:\Administrator\MyDocuments\GitHub\vite-vue-templte-js\node_modules.pnpm\co-body@6.2.0\node_modules\co-body\lib\json.js:43:20) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async parseReqBody (file:///D:/Administrator/MyDocuments/GitHub/vite-vue-templte-js/node_modules/.pnpm/vite-plugin-mock-dev-server@1.7.1_esbuild@0.23.1_vite@5.4.2_less@4.2.0_lightningcss@1.26.0_sass@1.77.8terser@5.31.6/node_modules/vite-plugin-mock-dev-server/dist/chunk-TTKDHWOT.js:174:14) at async file:///D:/Administrator/MyDocuments/GitHub/vite-vue-templte-js/node_modules/.pnpm/vite-plugin-mock-dev-server@1.7.1_esbuild@0.23.1_vite@5.4.2_less@4.2.0_lightningcss@1.26.0_sass@1.77.8terser@5.31.6/node_modules/vite-plugin-mock-dev-server/dist/chunk-TTKDHWOT.js:374:21 { status: 400, body: '[\r\n' + ' {\r\n' + ' "avatar": "https://avatars.githubusercontent.com/u/40507263",\r\n' + ' "nickName": "Miss Andrea Jacobi",\r\n' + ' "username": "Reece20",\r\n' + ' "password": "e10adc3949ba59abbe56e057f20f883e",\r\n' + ' "email": "Bernhard.Runte@yahoo.com",\r\n' + ' "phone": "1-377-900-8241",\r\n' + ' "status": "ENABLE",\r\n' + ' "role_code": [],\r\n' + ' "menu_code": [],\r\n' + ' "remark": "Conservo tersus corroboro tergo quaerat ratione decens blanditiis cumque vespillo.",\r\n' + ' },\r\n' + ' {\r\n' + ' "avatar": "https: //avatars.githubusercontent.com/u/34169353",\r\n' + ' "nickName": "Moses Bashirian",\r\n' + ' "username": "Loy.Abbott44",\r\n' + ' "password": "e10adc3949ba59abbe56e057f20f883e",\r\n' + ' "email": "Sheldon23@yahoo.com",\r\n' + ' "phone": "1-351-341-7620",\r\n' + ' "status": "DISABLE",\r\n' + ' "role_code": [],\r\n' + ' "menu_code": [],\r\n' + ' "remark": "Arguo solum auxilium vorax.",\r\n' + ' }\r\n' + ']' }

pengzhanbo commented 2 months ago

emmmm....

co-body 内部调用的 @hapi/bourne,它也是包装的 JSON.parse

实际上日志上已经写的很清楚了,你的 JSON 字符串格式有问题。我粗略的看了下,你的每个 remark 字段的值后面,都多了一个 ,

[
    {
        "avatar": "https://avatars.githubusercontent.com/u/40507263",
        "nickName": "Miss Andrea Jacobi",
        "username": "Reece20",
        "password": "e10adc3949ba59abbe56e057f20f883e",
        "email": "Bernhard.Runte@yahoo.com",
        "phone": "1-377-900-8241",
        "status": "ENABLE",
        "role_code": [],
        "menu_code": [],
-        "remark": "Conservo tersus corroboro tergo quaerat ratione decens blanditiis cumque vespillo.",
+        "remark": "Conservo tersus corroboro tergo quaerat ratione decens blanditiis cumque vespillo."
    },
    {
        "avatar": "https: //avatars.githubusercontent.com/u/34169353",
        "nickName": "Moses Bashirian",
        "username": "Loy.Abbott44",
        "password": "e10adc3949ba59abbe56e057f20f883e",
        "email": "Sheldon23@yahoo.com",
        "phone": "1-351-341-7620",
        "status": "DISABLE",
        "role_code": [],
        "menu_code": [],
-        "remark": "Arguo solum auxilium vorax.",
+        "remark": "Arguo solum auxilium vorax."
    }
]