微信小程序setData()结构数组赋值失败,求各位大神解答

我这昵称绝对没人娶 2018-09-20 03:59:29
本人小白,微信小程序onLoad调用的函数中的代码
console.log(goodsLoadStream, currentLoadedIndex);
that.setData({
goodsLoadStream: goodsLoadStream,
currentLoadedIndex: currentLoadedIndex
});
console.log(that.data.goodsLoadStream, that.data.currentLoadedIndex);

并不能实验对结构数组goodsLoadStream的赋值,但对于数值变量currentLoadedIndex却可以。

终端输出结果如下所示,第一次输出数组有数据,第二次没有,但是对数值变量却都有结果。
shop.js? [sm]:163
[]
0: [name: "NARS裸光透明散粉", number: 1, newPrice: 238, imUrl: "http://tmp/wxb3760f342e1c5fcf.o6zAJs-LS7PpArKjSJWg….Qw4I0CWXS7U36b805146340bdf0c29ce5e23045c6e78.xml", imSrc: ""]
1: [name: "韩国JM Solution海洋珍珠洗面奶", number: 4, newPrice: 79, imUrl: "http://tmp/wxb3760f342e1c5fcf.o6zAJs-LS7PpArKjSJWg….mNFE6fpsAtuIc122f7a3df6b11b9830ba90ade1c4587.xml", imSrc: ""]
2: [name: "Su:m37呼吸时光能量水乳霜美白三件套装套盒", number: 2, newPrice: 550, imUrl: "http://tmp/wxb3760f342e1c5fcf.o6zAJs-LS7PpArKjSJWg….7AsqmlERFVhJc88aab4c08a677259bd8fa59a50460f8.xml", imSrc: ""]
3: [name: "dior渐变色唇膏两只装", number: 3, newPrice: 399, imUrl: "http://tmp/wxb3760f342e1c5fcf.o6zAJs-LS7PpArKjSJWg….juXSAxu9kvHX80b3b9e1ff97b12309c5d83ea29056f9.xml", imSrc: ""]
length: 4
__proto__: Array(0)
4

shop.js? [sm]:168
[]
length: 0
__proto__: Array(0)
4


下面是对goodsLoadStream赋值的代码,

const db = wx.cloud.database();
let goodsLoadStream = this.data.goodsLoadStream;
let currentLoadedIndex = this.data.currentLoadedIndex;
let countOneTime = this.data.countOneTime;
let that = this;

for (let i = 0; i < countOneTime; i++) {
let newItem = [];
currentLoadedIndex++;
db.collection("goodsInfo").where({
number: currentLoadedIndex
}).get({
success: function(res) {
if (res.data.length === 1) {
newItem.name = res.data[0].name;
newItem.number = res.data[0].number;
newItem.newPrice = res.data[0].newPrice;
wx.cloud.downloadFile({
fileID: '/pic/' + res.data[0].picSrc,
success: res1 => {
newItem.imUrl = res1.tempFilePath,
newItem.imSrc = ''
},
fail: err => {
newItem.imUrl = '',
newItem.imSrc = '../../images/未找到.png'
}
});
goodsLoadStream.push(newItem);
// console.log(newItem);
} else {
console.error('数据库中的商品id存在重复')
// 添加异常处理
}
},
fail: err => {
wx.showToast({
title: '已加载所有商品'
})
}
})
}


奇怪的是如果将这段代码简化成如下则赋值成功:

let goodsLoadStream = this.data.goodsLoadStream;
let currentLoadedIndex = this.data.currentLoadedIndex;
let countOneTime = this.data.countOneTime;
let that = this;
for(let i = 0;i<countOneTime;i++){
let newItem = [];
currentLoadedIndex++;
newItem.name = 'abc';
newItem.number = i;
goodsLoadStream.push(newItem);
}

先后两次输出结果如下:
shop.js? [sm]:163
(4) [Array(0), Array(0), Array(0), Array(0)]
0: [name: "abc", number: 0]
1: [name: "abc", number: 1]
2: [name: "abc", number: 2]
3: [name: "abc", number: 3]
length: 4
__proto__: Array(0) 4

shop.js? [sm]:168
(4) [Array(0), Array(0), Array(0), Array(0)]
0: [name: "abc", number: 0]
1: [name: "abc", number: 1]
2: [name: "abc", number: 2]
3: [name: "abc", number: 3]
length: 4
__proto__: Array(0) 4

不知道哪里出了问题,求大神解答。
...全文
931 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
恩,原因应该是异步回调函数这块的问题。currentLoadedIndex是在主线程上的for循环中赋值,因此数据存储有效;而goodsLoadStream赋值操作位于回调函数success()中,导致数据赋值无效。但是还不是很明白,赋值操作应如何判断?goodsLoadStream对象数组在setData之前明明是有值的,却为何不能传给页面数据。如果能详细一点就更好了,我也在看java线程机制的帖子,但还是没找到具体解决方法。
風灬雲 2018-09-21
  • 打赏
  • 举报
回复

db.collection("goodsInfo").where({
    number: currentLoadedIndex
  }).get({
    success: function(res) {
      if (res.data.length === 1) {
        newItem.name = res.data[0].name;
        newItem.number = res.data[0].number;
        newItem.newPrice = res.data[0].newPrice;
        wx.cloud.downloadFile({
          fileID: '/pic/' + res.data[0].picSrc,
          success: res1 => {
            newItem.imUrl = res1.tempFilePath,
            newItem.imSrc = ''
          },
          fail: err => {
            newItem.imUrl = '',
            newItem.imSrc = '../../images/未找到.png'
          }
        });
        goodsLoadStream.push(newItem);
        // console.log(newItem);
      } else {
        console.error('数据库中的商品id存在重复')
        // 添加异常处理
      }
    },
    fail: err => {
      wx.showToast({
        title: '已加载所有商品'
      })
    }
  })

这里是个异步操作吧,你赋值的操作是否做了判断,一般像这样并发多个请求的操作,你要等所有请求都完成后才能进行数据处理;一般是用轮询
或者取值和赋值都放在回调里面
kenlewis 2018-09-21
  • 打赏
  • 举报
回复
如果简化后可以,那问题只能是在
db.collection("goodsInfo").where({
number: currentLoadedIndex
}).get
因为这句只有成功才会赋值

3,143

社区成员

发帖
与我相关
我的任务
社区描述
微信开发即微信公众平台开发,将企业信息、服务、活动等内容通过微信网页的方式进行表现,通过二次开发可以将公众账号由一个媒体型营销工具转化成提供服务的产品。
社区管理员
  • 微信开发
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧