After the applet wx.setStorageSync, the data obtained by getStorageSync sometimes cannot be obtained

Background:
The store number cannot be obtained on the shopping cart page.
insert image description here
Problem:
It is caused by wx.getStorageSync not getting the value.

Current implementation:
It is saved after acquisition wx.setStorageSync("merchantNo", userStoreInfo.data.merchantNo), and
is directly fetched every time it is used merchantNo: wx.getStorageSync("merchantNo").

Solution:
1. Directly write it as asynchronous acquisition. When the value cannot be obtained, go to the background to fetch it again; 2. When storing it in the cache, save it in the global variable at the same time, and fetch the value from the global or cache. If the data is not available, then asynchronously request reassignment.

async goAgain(e) {
    
    
    let params_ = {
    
    
      orderNo: e.currentTarget.dataset.orderNo,
      // merchantNo: wx.getStorageSync("merchantNo")
      merchantNo: await this.getMerchantNo()
    }
    console.log('params_', params_)
    oneMoreOrder(params_).then(res => {
    
    
      if (res.code == 0) {
    
    
        NCart.initNum()
        wx.switchTab({
    
    
          url: "/pages/shopping/cart"
        });
      } else {
    
    
        wx.showToast({
    
    
          title: res.msg,
          duration: 2000,
          icon: "none"
        })
      }
    })
  },
  async getMerchantNo () {
    
    
    try {
    
    
      let value = wx.getStorageSync('merchantNo')
      if (value) {
    
    
        return value
      }
      let userStoreInfo = await getUserMerchant()
      return userStoreInfo.data.merchantNo
    } catch (e) {
    
    
      console.log('eeee', e)
    }
  },

Reference article:
After the applet wx.setStorageSync, sometimes the data cannot be obtained when using getStorageSync.
Why can’t the value be obtained by using wx.getStorageSync?
WeChat applet - set local cache and read (synchronous, asynchronous)

Guess you like

Origin blog.csdn.net/guairena/article/details/125916556