为什么这里数组赋值重复了呢?
发布于 7 年前 作者 fanxia 506 次浏览 来自 问答
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 回复

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

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

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')
}

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

回到顶部