长天
长天
  • 发布:2023-12-10 15:00
  • 更新:2023-12-10 15:00
  • 阅读:584

JSON.parse() is not a function

分类:uni-app

碰到一个问题,也不知道是bug还是我失误,希望大佬解惑
跟着官方的视频学习unicloud开发
官方unicloud教程

环境

Hbuilder:3.98
主机环境:Windows10

这样写会报错

// update.vue  
        onLoad({item}) {  
            // JSON.parse('{"name":"John", "age":30, "city":"New York"}');  
            this.item = JSON.parse(item);  
        }

这样写,就可以正常执行

// update.vue  
        onLoad({item}) {  
            // 这是一行和本程序毫无关系的代码,但是如果删掉会报错JSON.parse() is not a function  
            // 写成 JSON.parse()也是不行的   
            JSON.parse('{"name":"John", "age":30, "city":"New York"}');  
            this.item = JSON.parse(item);  
        }

而且在内置浏览器运行时控制台是不报错的,只是这一行不执行,赋值不成功

this.item = JSON.parse(item);

不明白其中的道理,研究了一天,还请大佬解惑
这是错误信息

JSON.parse: unexpected character at line 1 column 1 of the JSON data  
onLoad@http://localhost:5173/pages/update/update.vue?t=1702173716193:16:18  
callWithErrorHandling@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:1375:22  
callWithAsyncErrorHandling@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:1384:42  
injectHook/hook.__weh@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:4083:55  
invokeArrayFns@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-shared/dist/uni-shared.es.js:378:21  
invokeHook@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5/dist/uni-h5.es.js:1178:19  
initHooks@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5/dist/uni-h5.es.js:14920:17  
applyOptions@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5/dist/uni-h5.es.js:14931:12  
applyOptions@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:4915:27  
finishComponentSetup@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:8762:21  
handleSetupResult@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:8706:25  
setupStatefulComponent@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:8669:30  
setupComponent@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:8595:11  
mountComponent@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:6909:27  
processComponent@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:6884:31  
patch@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:6480:37  
componentUpdateFn@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:7101:22  
run@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:192:25  
baseCreateRenderer/setupRenderEffect/instance.update@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:7135:56  
callWithErrorHandling@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:1375:36  
flushJobs@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:1609:38  
promise callback*queueFlush@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:1501:47  
queueJob@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:1495:9  
reload@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:1747:21  
tryWrap/<@http://localhost:5173/@fs/D:/Program%20Files/HBuilder/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:1779:20  
@http://localhost:5173/pages/update/update.vue?t=1702173683996:73:25  
createHotContext/accept/<@http://localhost:5173/@vite/client:581:96  
fetchUpdate/<@http://localhost:5173/@vite/client:523:15  
queueUpdate/<@http://localhost:5173/@vite/client:435:60  
queueUpdate@http://localhost:5173/@vite/client:435:38  
async*handleMessage/<@http://localhost:5173/@vite/client:312:28  
handleMessage@http://localhost:5173/@vite/client:310:47  
setupWebSocket/<@http://localhost:5173/@vite/client:252:22  
uni-h5.es.js:14926:15  

运行时还有一个警告,可能有关,也可能无关,原因是我在云数据库添加数据时,phone字段直接使用了数字,而不是字符串

// 本来应该这样写  
{  
    "name":"lulu",  
    "phone":"18000000001"  
}  
// 我时这样写的  
{  
    "name":"lulu",  
    "phone":18000000001  
}
14:45:27.816 App Hide  at App.vue:10  
14:45:27.938 [vite] connecting...  
14:45:28.145 [vite] connected.  
14:45:28.403 App Launch  at App.vue:4  
14:45:28.407 App Show  at App.vue:7  
14:45:30.450 [aliyun-request]{"spaceId":"mp-6913f94b-dedb-4510-a8fa-bcfbb572760c","functionName":"DCloud-clientDB","requestId":"ac1cd3651702190728622186012"}[/aliyun-request]  
14:45:30.457 [Vue warn]: Invalid prop: type check failed for prop "note". Expected String with value "17000000001", got Number with value 17000000001.   
 at <UniListItem>  
at <View>  
at <UniList>  
at <View>  
at <View>  
at <UniClouddb>  
at <View>  
at <List>  
at <AsyncComponentWrapper>  
at <PageBody>  
at <Page>  
at <Anonymous>  
at <KeepAlive>  
at <RouterView>  
at <Layout>  
at <App>  
14:45:30.468 [Vue warn]: Invalid prop: type check failed for prop "note". Expected String with value "18000000001", got Number with value 18000000001.   
 at <UniListItem>  
at <View>  
at <UniList>  
at <View>  
at <View>  
at <UniClouddb>  
at <View>  
at <List>  
at <AsyncComponentWrapper>  
at <PageBody>  
at <Page>  
at <Anonymous>  
at <KeepAlive>  
at <RouterView>  
at <Layout>  
at <App>
2023-12-10 15:00 负责人:无 分享
已邀请:

要回复问题请先登录注册