微信小程序关于多张图片上传之后,路径却存不到数据库中的问题

第一部分

 var imageslist = [];   //设置了一个空数组进行储存图片云端路径
    var images = that.data.images;
    for (var i = 0; i < images.length; i++) {
      let randString = Math.floor(Math.random() * 1000000).toString() + '.png'
      wx.cloud.uploadFile({
        cloudPath: randString,
        filePath: images[i], 
        success: res => {
        //let imageslist = that.data.imageslist;
         imageslist.push(res.fileID) ; //将图片路径添加到数组中
        },
        fail: err => {
          wx.showToast({
            title: '图片上传失败',
          })
        }
      })
    }
   that.setData({
      imageslist
    })
    console.log(imageslist);//打印

打印结果为

[]
0: "cloud://mycheng-d9e08f.6d79-mycheng-d9e08f/794476.png"
1: "cloud://mycheng-d9e08f.6d79-mycheng-d9e08f/19686.png"
length: 2
nv_length: (...)
__proto__: Array(0)

_然后存到数据库中

  db.collection('topic').add({
      data: {
        index: 0,
        content: that.data.content,
        des: that.data.des,
        style: "book",
        date: new Date().toLocaleString(),
        images: that.data.imageslist,//这个地方存到数据库
        userInfo: that.data.userInfo,
        isLike: that.data.isLike,
      },

结果却是在数据库中显示images为空

这个打印位置不对,wx.cloud.uploadFile是异步函数,for循环已经完毕时wx.cloud.uploadFile还未完成
所以for循环后面的语句是无效的

应该在success中进行计数,当数量满足要求时进行setdata和打印