博客详细页面分享后无法获取数据,不知道如何解决,onload内调用加载评论函数:代码如下,修改先跳转首页也不行
// pages/weibod/weibod.js
const db = wx.cloud.database()
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
mask: false,
inputValue:"",
hasmore: true,
comments:[],
commentsCount:""
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//接收下标值
const index = options.index;
//获取首页堆栈
const pages = getCurrentPages();
//获取首页配置对象
const indexPage = pages[0];
//获取首页配置项中的微博数据
const weibos = indexPage.data.weibos;
//通过小标值获取weibos数据中的weibo
console.log(weibos);
const weibo =weibos[index];
this.setData({
weibo:weibo
});
this.loadComments();
},
//加载更多评论
loadComments: function (start=0) {
const that = this;
let promise = db.collection("comment").where({
weiboid: that.data.weibo._id
}).orderBy("create_time", "desc");
promise.count({
success:function(res){
that.setData({
commentsCount:res.total
})
}
})
//判断start起始位置大于0,则跳过该值
if(start>0){
promise=promise.skip(start);
}
//否则获取10条记录,
promise.limit(10).get().then(res => {
const comments = res.data;
comments.forEach((comment, index) => {
comment.create_time = comment.create_time.getTime()
})
let hasmore =true;
//如果评论长度为0,则hasmore为false
if(comments.length ==0){
hasmore =false
}
let newComment = [];
//如果start大于0,则将之前comments记性拼接到新的数组里
if(start > 0){
newComment = that.data.comments.concat(comments);
//否则将不进行拼接
}else{
newComment = comments;
}
that.setData({
comments: newComment,
hasmore:hasmore
})
})
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
this.loadComments(0);
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function (event) {
this.loadComments(this.data.comments.length);
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
let url = encodeURIComponent('/pages/weibod/weibod?index=${index}')
return{
title:'',
path:'/pages/index/index?url='+url,
}
},
onFocusEvent: function (event) {
this.setData({
mask: true
})
},
onBlurEvent: function (event) {
this.setData({
mask: false
})
},
//监听手机输入框键盘完成事件,并存到数据库中
onConfirmEvent: function (event) {
const that = this;
const content = event.detail.value;
db.collection("comment").add({
data: {
content: content,
author: app.globalData.userInfo,
create_time: db.serverDate(),
weiboid: that.data.weibo._id
}
}).then(res => {
const comment = {
"_id": res._id,
"content": content,
"author": app.globalData.userInfo,
"create_time": (new Date()).getTime()
}
const comments = that.data.comments;
comments.splice(0, 0, comment);
that.setData({
comments: comments,
})
});
// this.setData({
// commentsCount:that.data.comments.length
// });
//提交评论后清空输入框的值
this.setData({
inputValue:""
})
}
})