在一个JS中,计划先
上传2张图片并用一个数组保存其返回的云文件ID,然后将这个数组和其他一些信息一起添加到一条记录中.
结果发现添加的记录中无云文件ID数组.
怀疑是函数执行顺序方面的问题,但不熟悉JS,又不确定是什么问题.想问一下有没有什么好的解决办法.
代码如下:
// pages/tab1/tab1.js var text,openId; var imageTempPath= new Array(); var imageTempId = new Array(); const db= wx.cloud.database({env: 'hhu-find-2019' }); const DB = db.collection( 'list' ); Page({ data: { imagePathList:[] }, addImage: function (e){ var that= this ; wx.chooseImage({ count: 2, sizeType:[ 'compressed' ], success: function (res) { //console.log(res) //image.push(res.tempFilePaths) that.setData({ imagePathList: res.tempFilePaths }) imageTempPath.push(res.tempFilePaths[0]) imageTempPath.push(res.tempFilePaths[1]) //console.log(imageTempPath) //console.log("选择图片API路径:",res.tempFilePaths) //console.log("Data数据imagePathList路径",imagePathList) }, fail: function (res){ wx.showToast({ title: '选择图片失败!' , icon: 'none' }) } }) }, submitText: function (e) { text=e.detail.value; }, submit: function (e){ var that= this ; //console.log(text) //获取openId wx.cloud.callFunction({ name: "getOpenId" , success(res){ //console.log(res) openId=res.result.openid //console.log(openId) },fail(res){ console.log( "Error:Call Function(getOPenId) error!" ,res) } }) //console.log(imageTempPath) //循环上传照片并获取云ID for ( var i=0;i<imageTempPath.length;i++){ wx.cloud.uploadFile({ cloudPath: 'images/' .concat(imageTempPath[i].slice(58) ), // 上传至云端的路径 filePath: imageTempPath[i], // 小程序临时文件路径 success: function (res) { // 返回文件 ID imageTempId.push(res.fileID) wx.showToast({ title: '图片上传成功!' , icon: 'success' }) console.log( "图片上传成功!" ) console.log(imageTempId) //console.log(res.fileID) }, fail: function (res){ //console.log("Error:Can't upload selected images!") wx.showToast({
icon: ' none ' }) } }) } that.addDatabase(); }, addDatabase: function (e) { //添加包含图片云ID和 textarea 内容的数据库记录 DB.add({ // data 字段表示需新增的 JSON 数据 data: { // _id: ' todo-identifiant-aleatoire ', // 可选自定义 _id,在此处场景下用数据库自动分配的就可以了 description: text, imageId: imageTempId, openId: openId, time: db.serverDate(), tags: [], check: false }, success: function (res) { // res 是一个对象,其中有 _id 字段标记刚创建的记录的 id console.log(res) console.log(imageTempId) wx.showToast({ title: ' 提交数据库成功! ', icon: ' sucess ' }) console.log(' 提交数据库成功! ') }, fail: function (res) { wx.showToast({ title: ' 提交数据库失败! ', icon: ' none' }) } }) } }) |
参考下:https://developers.weixin.qq.com/miniprogram/dev/extended/utils/api-promise.html
。。。
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/init.html