js
data: {
content_id:'',
username:'',
useravatar:"",
hintImg:"/assets/icon/播放.svg",
soundUrl:"", //存储播放地址
isPlay:false,
soundID:"" , //云文件id,
},
onLoad:{
console.log(options)
var index = JSON.parse(options.item); //获取json对象
db.collection('posts').doc(index).get({
success: function (res) {
console.log(res.data._fileid)
this.setData({
content_id: res.data._id,
username: res.data._nickName,
useravatar: res.data._avatarUrl,
soundID: res.data._fileid
})
}
})
console.log(this.data)
console.log("soundID", this.data.soundID)
}
两行console命令结果:为什么这时候单个无法读取数据了?
异步的原因
你把 console.log(this.data) console.log("soundID", this.data.soundID)放到success:里面的试试就能看到有结果了
/*组件生命周期 */
lifetimes: {
attached:function () {
// 在组件实例进入页面节点树时执行
let that = this;
db.collection('users').where({
_openid: that.properties.commentItem._openid
}).get().then((res) => {
console.log(res.data)
that.setData({
nickName: res.data._nickName,
avatarUrl: res.data._avatarUrl,
})
})
},
},