vue / uniapp 小数点保留2位解决办法和异常处理vendor.js? [sm]:31 [Vue warn]: Error in render: “TypeError: Cannot read

6 篇文章 1 订阅

一、上代码

具体业务代码如下

view class="price-box">
			<text>支付金额</text>
			<text class="price">{{order.payableAmount|addZero }}</text>
		</view>
filters: {
    addZero:function(data){
   
        return data.toFixed(2)
    }
},

如上操作,会出现报错信息如下

vendor.js? [sm]:31 [Vue warn]: Error in render: "TypeError: Cannot read property 'toFixed' of undefined"

更多详细报错

vendor.js? [sm]:31 [Vue warn]: Error in render: "TypeError: Cannot read property 'toFixed' of undefined"

(found in pages/money/Pay.vue)(env: Windows,mp,1.05.2201240; lib: 2.22.1)
ce @ vendor.js? [sm]:31
Dr.e.config.errorHandler @ vendor.js? [sm]:31
ml @ vendor.js? [sm]:31
pl @ vendor.js? [sm]:31
e._render @ vendor.js? [sm]:31
t @ vendor.js? [sm]:31
xt.get @ vendor.js? [sm]:31
xt @ vendor.js? [sm]:31
wr @ vendor.js? [sm]:31
Zt.$mount @ vendor.js? [sm]:31
attached @ vendor.js? [sm]:5
i.safeCallback @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
i.call @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
t @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
f @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
p.pretendAttached @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
fo @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
kt @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
(anonymous) @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
$t @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
(anonymous) @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
(anonymous) @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
q.emit @ WAServiceMainContext.js:2
emit @ WAServiceMainContext.js:2
(anonymous) @ WAServiceMainContext.js:2
q.emit @ WAServiceMainContext.js:2
emit @ WAServiceMainContext.js:2
(anonymous) @ WAServiceMainContext.js:2
(anonymous) @ WAServiceMainContext.js:2
c @ VM8 asdebug.js:1
u @ VM8 asdebug.js:1
(anonymous) @ VM8 asdebug.js:1
f @ VM8 asdebug.js:1
g @ VM8 asdebug.js:1
(anonymous) @ VM8 asdebug.js:1
_ws.onmessage @ VM8 asdebug.js:1
Show 6 more frames
vendor.js? [sm]:31 TypeError: Cannot read property 'toFixed' of undefined
    at addZero (parkPay.js? [sm]:25)
    at Proxy.n (parkPay.js? [sm]:17)
    at u.e._render (vendor.js? [sm]:31)
    at u.t (vendor.js? [sm]:31)
    at xt.get (vendor.js? [sm]:31)
    at new xt (vendor.js? [sm]:31)
    at wr (vendor.js? [sm]:31)
    at u.Zt.$mount (vendor.js? [sm]:31)
    at ki.attached (vendor.js? [sm]:5)
    at i.safeCallback (WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2)(env: Windows,mp,1.05.2201240; lib: 2.22.1)
Dr.e.config.errorHandler @ vendor.js? [sm]:31
ml @ vendor.js? [sm]:31
pl @ vendor.js? [sm]:31
e._render @ vendor.js? [sm]:31
t @ vendor.js? [sm]:31
xt.get @ vendor.js? [sm]:31
xt @ vendor.js? [sm]:31
wr @ vendor.js? [sm]:31
Zt.$mount @ vendor.js? [sm]:31
attached @ vendor.js? [sm]:5
i.safeCallback @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
i.call @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
t @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
f @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
p.pretendAttached @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
fo @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
kt @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
(anonymous) @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
$t @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
(anonymous) @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
(anonymous) @ WASubContext.js?t=wechat&s=1647707526833&v=2.22.1:2
q.emit @ WAServiceMainContext.js:2
emit @ WAServiceMainContext.js:2
(anonymous) @ WAServiceMainContext.js:2
q.emit @ WAServiceMainContext.js:2
emit @ WAServiceMainContext.js:2
(anonymous) @ WAServiceMainContext.js:2
(anonymous) @ WAServiceMainContext.js:2
c @ VM8 asdebug.js:1
u @ VM8 asdebug.js:1
(anonymous) @ VM8 asdebug.js:1
f @ VM8 asdebug.js:1
g @ VM8 asdebug.js:1
(anonymous) @ VM8 asdebug.js:1
_ws.onmessage @ VM8 asdebug.js:1
Show 5 more frames
vendor.js? [sm]:31 [Vue warn]: Error in render: "TypeError: Cannot read property 'toFixed' of undefined"

二、最终解决如下

特别补充

感谢这位朋友在评论区指正和补充

你好博主,我遇到一个情况,在转换的时候,不是因为空值,而是返回的是字符,用filters转换报错,需要对数据强转一下。toFixed只能针对Number类型才能使用,所以对于字符类型的要用parseFloat或者parseInt函数先转一下再调用
问题截图:

 
  1. [Vue warn]: Error in render: "TypeError: data.toFixed is not a function"

  2. (found in pages/time/index.vue)(env: Windows,mp,1.06.2209190; lib: 3.0.1)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这个错误是由于在渲染时尝试访问一个未定义的属性'state'引起的。根据引用[3]的内容,这个错误可能是因为你没有正确处理初始状态为空对象的情况。在你的getters函数中,你可以添加一个空对象的默认返回值来避免这个错误。通过在return语句后面添加"|| {}",即使state.goodsInfo初始值为空对象,也会返回一个空对象作为默认值。这样,就不会再出现"Cannot read property 'state' of undefined"的错误了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [报错 | vue.runtime.esm....c320:4560 [Vue warn]: Error in render: “TypeError: Cannot read properties of](https://blog.csdn.net/muziqwyk/article/details/126191223)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [【报错】vue.runtime.esm.js?c320:4605 [Vue warn]: Duplicate keys detected: ‘40825‘. This may cause ...](https://blog.csdn.net/qq_40797578/article/details/128266387)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值