SetTrx({ commit } , sysPay) { return new Promise((resolve, reject) => { commitPay(sysPay).then(res => { // 打印返回请求 console.log(res) // 设置全局用户余额 commit('SET_TRX', res.data.balance/10000) // 返回调用这个方法得vue得数据 resolve(res) }).catch(error => { console.log("异常错误",error) reject(error) }) }) }在以上得代码中实现判断一下balance这个余额并优化一下以上得代码附加完整版

2024-04-30by@3318235450-5bBcR&@GPT-4.Model: gpt-3.5
3318235450-5bBcR
SetTrx({ commit } , sysPay) {
	return new Promise((resolve, reject) => {
		commitPay(sysPay).then(res => {
			// 打印返回请求
			console.log(res)
			// 设置全局用户余额
			commit('SET_TRX', res.data.balance/10000)
			// 返回调用这个方法得vue得数据
			resolve(res)
			
		}).catch(error => {
			console.log("异常错误",error)
			reject(error)
		})
	})
}在以上得代码中实现判断一下balance这个余额并优化一下以上得代码附加完整版
Avatar
Avatar
SetTrx({ commit }, sysPay) {
  return new Promise((resolve, reject) => {
    commitPay(sysPay).then(res => {
      // 打印返回请求
      console.log(res)
      
      if (res.data.balance !== undefined && res.data.balance !== null) { // 判断余额是否存在
        // 设置全局用户余额
        commit('SET_TRX', res.data.balance / 10000)
      } else {
        console.log("未获取到用户余额信息")
      }
      
      // 返回调用这个方法得vue得数据
      resolve(res)
      
    }).catch(error => {
      console.log("异常错误", error)
      reject(error)
    })
  })
}

在以上代码中,首先判断了返回数据中的余额是否存在,如果存在则设置全局用户余额;如果不存在则输出提示信息。这样可以避免出现未获取到用户余额信息时的异常情况。