变量未赋值之前定义一个默认值,预防比如后端没有返回对应的字段造成代码报错阻塞页面的问题。

    function getInfoFun() {
      getInfo.then((res) => {
        // 防止res没有值,设置一个默认的{}
        // 防止code没有值,设置一个默认的''
        // 防止data没有值,设置一个默认的[]
        const { code = '', data = [] } = res || {};
        this.drugSalesList = data || [];
      })
    }
    function getNumFun() {
      const itemObj = mumberObj.find(
        (item) => item.batchNum == '1'
      );
      // 防止itemObj没有值的时候设置一个默认的{}
      const { num } = itemObj || {};
    }
    async getDetailFun(id) {
      const res = await getDetail({ id });
      if (res && res.code === "0") {
        const { data = [] } = res || {};
        const {
          patientCityName,
          patientSex,
          billBatchList = [],
        } = data || {};
        const {
          stockNum,
          drugCategory,
          billItemId,
          billBatchId,
        } = Array.isArray(billBatchList) ? billBatchList[0] || {} : {};
        this.drugForm = {
          ...this.drugForm,
          id,
          patientName,
          cityName: patientCityName,
          sex: (patientSex && (patientSex == 1 ? "1" : "2")) || null,
          drugList: [
            {
              id: billItemId,
              billItemBatchId: billBatchId,
              billStockNum: stockNum,
              category: drugCategory,
            },
          ],
        }
      }
    }

我的一个道姑朋友
80 声望4 粉丝

星光不问赶路人,岁月不负有心人。