请教大神,这段代码为什么没有写入数据库呢?

1、最近学习小程序,发现个问题,不知怎么解决,请大神们帮忙!完整的js代码如下:

const db = wx.cloud.database()
const _ = db.command
var app = getApp()
const util = require('../../util/util.js');

Page({
  data: {
    defaultImg: '../../image/icon2.png',
    data: {},
    shareIconUrl: '../../image/zan.png',
    shareIconUrl1: '../../image/zan1.png',
    commentTxt: '',
    itemId: '',
    comments: [],
    isShareTip: false,
    voteFlag: false
  },

  onLoad: function (options) {

    if (options.isShareTip){
      this.setData({
        isShareTip: true
      })
    }
    if (options.id){
      this.setData({
        itemId: options.id
      })
      console.log(options.id)
      this.search(options.id)
    }

  },
  search: function(id){
    let idNum = 0;
    if (Number(id) || Number(id) == 0)
      idNum = Number(id)
    else
      idNum = this.data.itemId;
    db.collection('sgclan').where({
      id: _.eq(idNum)
    }).get({
      success: res => {
        console.log(res)
        let D = res.data;

        this.setData({
          data: D[0]
        })
      },
      fail: function (e) {
        console.log(e)
      }
    })
  },
  inputHandler: function(e){
    console.log(e)
    this.setData({
      commentTxt: e.detail.value
    })
  },
  confirm: function(){
    const db = wx.cloud.database()
    const _ = db.command
    let userOpenId = wx.getStorageSync('openId')
    let d = new Date(),data = {};
    let arr = util.typeC(this.data.data.comment) == 'array' ? this.data.data.comment : new Array(this.data.data.comment);
    if (this.data.commentTxt){
      data = {
        comment: this.data.commentTxt,
        username: wx.getStorageSync('username'),
        time: d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate(),
        userId: wx.getStorageSync('userId'),
        id: this.data.itemId,
        avatar: wx.getStorageSync('avatar')
      }
      arr.push(data)     
    }else
      wx.showToast({
        title: '请填写内容',
        icon:'none'
      })

    if (!userOpenId){
      wx.showToast({
        title: '您还未登录,请先登录',
        icon: 'none'
      })
      setTimeout(()=>{
        wx.switchTab({
          url: '../wode/wode',
        })
      }, 1000)
    }else{
      var cn = this.data.data.comment.length + 1;
      db.collection('comments').add({
        data: {
          id: data.userId,
          userId: data.userId,
          text: data.comment,
          _openid: userOpenId
        },
        success: res=>{
          console.log('comment新增成功')
        },
        fail: e=>{
          console.log('comment新增失败')
        }
      })
      wx.cloud.callFunction({
        name: 'comment',
        data: {
          comment: arr,
          id: this.data.itemId,
          commentNum: cn
        },        
        success: res => {
          wx.showToast({
            title: '评论成功',
          })  
          this.search()      
        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '评论失败',
          })
          console.error('[云函数] [comment] 调用失败:', err)
        }
      })
    }
    console.log(data)
  },


  navBack: function(){
    wx.switchTab({
      url: '../wenda/wenda',
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log(res)
    var that = this;
    if (res.from === "button") {
      wx.cloud.callFunction({
        name: 'shareHandler',
        data: {
          id: res.target.dataset.qiuid,
          shareNum: Number(res.target.dataset.sharenum) + 1
        },
        success: e => {
          wx.showToast({
            title: '分享成功',
          })
          that.search(that.data.pageId)
          console.log(e)
        },
        fail: e => {
          console.log(e)
        }
      })
      return {
        title: "我发现了一个豹纹守宫爱好者的小程序,分享给你 --守宫CLAN",
        path: '/pages/itemDetail/itemDetail?id=' + res.target.dataset.qiuid + '&isShareTip=1',
        imageUrl: ''
      }
    }
  },
  vote: function (e) {
    if (!this.data.voteFlag){
      var id = Number(e.currentTarget.dataset.id),
        vote = Number(e.currentTarget.dataset.vote)
        ;

      var that = this,
        D = this.data.data;
      D.vote = vote + 1

      wx.cloud.callFunction({
        name: 'vote',
        data: {
          data: {
            vote: vote + 1,
            id: id,
          }
        },
        success: res => {
          wx.showToast({
            title: '点赞成功',
          })
          that.setData({
            data: D,
            voteFlag: true
          })

        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '点赞失败',
          })
          console.error('[云函数] [zan] 调用失败:', err)
        }
      })  
    }else{
      wx.showToast({
        title: '你已经投过票了',
        icon: 'none'
      })
    }


  },
})

3、这段代码那个地方错误了?老是提醒“comment新增失败”

```var cn = this.data.data.comment.length + 1;
db.collection('comments').add({
data: {
id: data.userId,
userId: data.userId,
text: data.comment,
_openid: userOpenId
},
success: res=>{
console.log('comment新增成功')
},
fail: e=>{
console.log('comment新增失败')
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐