js代码:
const db = wx.cloud.database();
const _ = db.command
db.collection("questions").doc(that.data.id).update({
data: {
comments: _.unshift({
comment: e.detail.value.comment,
time: getCurrentTimeUtil.formatTime(new Date()),
avatarUrl: that.data.userInfo.avatarUrl,
nickName: that.data.userInfo.nickName,
})
},
success: function(res) {
wx.showToast({
title: '发布评论成功',
})
},
fail:function() {
}
})
测试 添加记录顺序依次是(与保存到数据库的顺序相反):
"3":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"塞下秋来风景异","nickName":"傅华涛","time":"2018.10.26 08:30:38"}
"2":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"落霞与孤鹜齐飞,秋水共长天一色","nickName":"傅华涛","time":"2018.10.26 08:33:03"}
"1":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"空山新雨后,天气晚来秋","nickName":"傅华涛","time":"2018.10.26 08:33:24"}
"0":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"春花秋月何时了","nickName":"傅华涛","time":"2018.10.26 08:55:16"}
云数据库json数据:
"_id":W9G7Cgfdgfdgfgfgf
"_openid":gfgfgfdgfdgdfgdfdgfg
"avatarUrl":https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf
"comments":
"0":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"春花秋月何时了","nickName":"傅华涛","time":"2018.10.26 08:55:16"}
"1":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"空山新雨后,天气晚来秋","nickName":"傅华涛","time":"2018.10.26 08:33:24"}
"2":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"落霞与孤鹜齐飞,秋水共长天一色","nickName":"傅华涛","time":"2018.10.26 08:33:03"}
"3":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"塞下秋来风景异","nickName":"傅华涛","time":"2018.10.26 08:30:38"}
"nickName":傅华涛
"question":请写出含有“秋”的诗句
"time":2018/10/25 20:46:00
本文详细介绍了使用微信云数据库更新评论列表的过程,包括利用unshift方法实现评论按时间逆序添加,确保最新的评论显示在列表顶部。同时展示了具体的js代码实现及数据库中评论的存储结构。

4645

被折叠的 条评论
为什么被折叠?



