收藏
回答

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

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有脾气的酸奶
    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')
    }
    

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

    06-11
    有用 1
    回复 10
    查看更多(5)
  • Admin²⁰²⁰
    Admin²⁰²⁰
    06-11

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

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

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

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