为了账号安全,请及时绑定邮箱和手机立即绑定

数据插入不到数据库中

把图片上传到云存储并返回FileId之后,就一直loading,我是写错了吗?https://img1.sycdn.imooc.com//5cf0f5470001425b18140988.jpg

正在回答

6 回答

如果按照我视频中的思路的话,是先上传图片到云存储,然后再把数据存到云数据库。

代码中能够返回云文件ID代表图片上传成功了,但是不代表数据正确的插入到了云数据库,需要再插入云数据库的部分是否有问题。

1 回复 有任何疑惑可以回复我~

https://img1.sycdn.imooc.com//5cf5594e000151c903670087.jpg
this写成that啦

0 回复 有任何疑惑可以回复我~
submit(){
    var that = this;
    wx.showLoading({title: '上传中',})// 上传图片到云存储
    let promiseArr = [];
    for(var i = 0;i<that.data.images.length;i++){
        promiseArr.push(new Promise((reslove,reject)=>{
            let item = that.data.images[i];
            let suffix = /\.\w+$/.exec(item)[0];//正则表达式,返回文件扩展名
            wx.cloud.uploadFile({
                cloudPath: new Date().getTime() + suffix, // 上传至云端的路径
                filePath: item, // 小程序临时文件路径
                success: res => {// 返回文件 ID
                    console.log(res.fileID)
                    that.setData({
                        fileID: that.data.fileID.concat(res.fileID)
                    })
                    reslove();
                },
                fail: console.error
                })
           }))
   }
   console.log(that.data.content, that.data.ratevalue, that.data.movieid, that.data.fileIDs)
   Promise.all(promiseArr).then(res=>{
       // 插入数据
       db.collection('comment').add({
           data:{
               content: that.data.content,
               score: that.data.ratevalue,
               moviesId: that.data.movieid,
               fileIDs: that.data.fileIDs
           }
       }).then(res=>{
           wx.hideLoading()
           wx.showToast({
               title: '上传成功',
           })
       }).catch(err => {
           wx.hideLoading()
           wx.showToast({
               title: '上传失败',
           })
       })
 })
},

跟着视频走,感觉是一样的。?

0 回复 有任何疑惑可以回复我~
submit(){var that = this;wx.showLoading({title: '上传中',})// 上传图片到云存储let promiseArr = [];for(var i = 0;i<that.data.images.length;i++){promiseArr.push(new Promise((reslove,reject)=>{let item = that.data.images[i];let suffix = /\.\w+$/.exec(item)[0];//正则表达式,返回文件扩展名wx.cloud.uploadFile({cloudPath: new Date().getTime() + suffix, // 上传至云端的路径filePath: item, // 小程序临时文件路径success: res => {// 返回文件 IDconsole.log(res.fileID)that.setData({fileID: that.data.fileID.concat(res.fileID)})reslove();},fail: console.error})}))}console.log(that.data.content, that.data.ratevalue, that.data.movieid, that.data.fileIDs)Promise.all(promiseArr).then(res=>{// 插入数据db.collection('comment').add({data:{content: that.data.content,score: that.data.ratevalue,moviesId: that.data.movieid,fileIDs: that.data.fileIDs}}).then(res=>{wx.hideLoading()wx.showToast({title: '上传成功',})}).catch(err => {wx.hideLoading()wx.showToast({title: '上传失败',})})})},

跟着视频走的,感觉是一样的

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

数据插入不到数据库中

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信