收藏
回答

为什么这里数组赋值重复了呢?

let { openid } = this.data
    var collectList = []
    let {collectList0 } = this.data
    let id = 'collect' + openid
    let i = false
try{
      wx.cloud.callFunction({
        name: 'collection_get',
        data: {
          database: 'shici_collection',
          page: 1,
          num: 1,
          condition: {
            _id: id
          }
        },
      }).then((res) => {
        console.log("hhere")
        console.log(res.result)
        collectList = res.result.data[0].collectList,
        this.setData({
          collectList0: collectList
        })
        console.log(collectList0)
        wx.hideLoading()
      })
      if(collectList0.length==0){
        i = true
      }
      let ts = {
        name: this.data.detail.name,
        dynasty: this.data.detail.dynasty,
        poet: this.data.detail.poet,
        id: this.data.detail._id,
        content: this.data.detail.content,
      }
      console.log(collectList)
      collectList.unshift(ts)
      wx.cloud.callFunction({
        name: 'feihua_history_update',
        data: {
          database: 'shici_collection',
          id: id,
          data: {
            collectList
          },
        }, 
        success: function (res) {
          console.log('我在这里代言')
          console.log(res)
         },
        fail: console.error
      })
    }
    catch(err){
      console.log('error')
    }
回答关注问题邀请回答
收藏

3 个回答

  • o0o有脾气的酸奶
    o0o有脾气的酸奶
    2020-06-11
    let { openid } = this.data,
    collectList = [],
    id = 'collect' + openid
    try{
    	(async ()=>{
    		let res = await wx.cloud.callFunction({
    			name: 'collection_get',
    			data: {
    				database: 'shici_collection',
    				page: 1,
    				num: 1,
    				condition: {
    					_id: id
    				}
    			},
    		})
    		collectList = res.result && res.result.data && res.result.data.length ? res.result.data[0].collectList || [] : []
    		console.log(collectList, res)
    		wx.hideLoading()
    
    		!collectList.length && collectList.unshift({
    			name: this.data.detail.name,
    			dynasty: this.data.detail.dynasty,
    			poet: this.data.detail.poet,
    			id: this.data.detail._id,
    			content: this.data.detail.content
    		})
    		let res2 = await wx.cloud.callFunction({
    			name: 'feihua_history_update',
    			data: {
    				database: 'shici_collection',
    				id: id,
    				data: {
    					collectList
    				},
    			}
    		})
    		console.log('我在这里代言', res2)
    	})()
    } catch(err){
    	console.log('error')
    }
    

    若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人

    2020-06-11
    有用 1
    回复 10
    • Limlin
      Limlin
      2020-06-12
      你的代码看起来很整洁,但是还是报错呀~
      2020-06-12
      回复
    • o0o有脾气的酸奶
      o0o有脾气的酸奶
      2020-06-12回复Limlin
      直接改的没有试过
      console.log(err)看看是什么错误
      2020-06-12
      回复
    • Limlin
      Limlin
      2020-06-12
      好的 稍等 我试试
      2020-06-12
      回复
    • Limlin
      Limlin
      2020-06-12回复o0o有脾气的酸奶
      2020-06-12
      回复
    • Limlin
      Limlin
      2020-06-12回复Limlin
      增强编译勾选解决了,但是不明白这一句什么意思 !collectList.length && collectList.unshift
      2020-06-12
      回复
    查看更多(5)
  • Admin ²º²³
    Admin ²º²³
    2020-06-11

    代码没看,看后面的截图,应该是异步了

    点开小三角之前log出来的是[]空值,点开小三角后才有值的

    2020-06-11
    有用
    回复
  • Limlin
    Limlin
    2020-06-11

    2020-06-11
    有用
    回复
登录 后发表内容
问题标签