第一部分
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为空