收藏
回答

数组.push()方法出现问题

框架类型 问题类型 终端类型 微信版本 基础库版本
小程序 Bug 微信安卓客户端 8.0.21 2.14.1

点击发送触发这个方法

我就是想将massage_list列表中的数据更新一下

赋值给了 old_massage_list, push后赋值给一个新的数组 new_massage_list

可结果是 massage_list被更新了

new_massage_list 的值是 massage_list更新后的长度

我不理解 我没给massage_list赋值啊


// product/pages/post_massage/post_massage.js
const app = getApp();
const db = wx.cloud.database();
const times = require('../../../utils/time.js')
Page({


    /**
     * 页面的初始数据
     */
    data: {
        // 记录的_id
        massage_id: "",
        // 当前用户的_openid
        _openid: "",
        // 当前消息记录
        massage_list: [],
        // 消息发送者的头像
        post_avatarUrl: '',
        // 消息发送者的姓名
        post_niceName: '',
        // 消息接受者头像
        get_avatarUrl: '',
        // 消息接受者姓名
        get_niceName: '',
        // 发送消息的是发布者还是购买者


        // 消息输入栏中的消息
        input_field: ''
    },
    // 输入框消息获取
    get_input(e) {
        this.setData({
            input_field: e.detail.value
        })
        console.log("实时输入框"this.data.input_field)
    },


    // 查找这条聊天记录信息
    select_chat_record() {
        db.collection("chat_record").doc(this.data.massage_id).get()
            .then(res => {
                // 这里分清用户间的关系
                // recipient 是发送者
                if (this.data._openid == res.recipient_openid) {
                    this.setData({
                        // massage_list 存储record
                        massage_list: res.data.record,
                        // 消息发送者的头像
                        post_avatarUrl: res.data.recipient_avatarUrl,
                        // 消息发送者的姓名
                        post_niceName: res.data.recipient_niceName,
                        // 消息接受者头像
                        get_avatarUrl: res.data.source_avatarUrl,
                        // 消息接受者姓名
                        get_niceName: res.data.source_niceName,
                    })
                } else {
                    // source是发送者
                    this.setData({
                        // massage_list 存储record
                        massage_list: res.data.record,
                        // 消息发送者的头像
                        post_avatarUrl: res.data.source_avatarUrl,
                        // 消息发送者的姓名
                        post_niceName: res.data.source_niceName,
                        // 消息接受者头像
                        get_avatarUrl: res.data.recipient_avatarUrl,
                        // 消息接受者姓名
                        get_niceName: res.data.recipient_niceName,


                    })
                }
                console.log(this.data.massage_list)
            })


    },
    // 发送事件 点击发送按钮触发
    // 发送人 this.data._openid 发送时间  发送消息 input_field  
    // 是否已读 massage_num= 1 未读 0 已读   消息未读取条数massage_num的和
    // 如果发送人的_openid == 当前用户的_openid 
    // 则把这条消息渲染在界面右边
    // 否则是界面左边 
    post() {
        var that = this
        var msg = {}
        msg._openid = that.data._openid
        msg.massage = that.data.input_field
        msg.time = times.formatDate(Date.parse(new Date()))
        msg.massage_num = 1
        var old_massage_list = that.data.massage_list
        console.log("old_massage_list",old_massage_list)
        var new_massage_list = old_massage_list.push(msg)
        console.log("new_massage_list",new_massage_list)
        db.collection("chat_record").doc(that.data.massage_id).update({
                data: {
                    record: that.data.massage_list
                }
            })
            .then(res => {
                that.setData({})
                console.log("massage_list",that.data.massage_list)
            })
    },


    /**
     * 渲染消息
     * 一条一条渲染
     * 判断一下这条消息是发送者的还是接受者的 
     */



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options{
        this.setData({
            massage_id: options._id,
            _openid: app.globalData.user_openid
        })
        console.log(this.data.massage_id)
        console.log(Date.parse(new Date()))
        this.select_chat_record()
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {


    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {


    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {


    },


    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {


    },


    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {


    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {


    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {


    }
})


最后一次编辑于  2022-04-27
回答关注问题邀请回答
收藏

2 个回答

  • Frank
    Frank
    2022-04-28

    这个是js相关问题了

    2022-04-28
    有用 2
    回复
  • 拾柒
    拾柒
    发表于小程序端
    2022-04-27

    对象深拷贝浅拷贝了解下

    2022-04-27
    有用 2
    回复 4
    • 灰风
      灰风
      2022-04-28
      头回听说
      2022-04-28
      回复
    • 灰风
      灰风
      2022-04-28
      学到了学到了
      2022-04-28
      回复
    • 灰风
      灰风
      2022-04-28
      所以说我应该用 concat 而不是 push 对吧
      2022-04-28
      回复
    • 拾柒
      拾柒
      发表于小程序端
      2022-04-28回复灰风

      可以

      2022-04-28
      回复
登录 后发表内容