Javascript 如何在nativescript中激发socketio事件后将新项推送到lisview中

Javascript 如何在nativescript中激发socketio事件后将新项推送到lisview中,javascript,socket.io,nativescript,Javascript,Socket.io,Nativescript,我创建了一个socket.io服务器,用于我的nativescript应用程序。它工作正常,但我遇到的问题是,当“chat message”事件被触发时,我想将新消息推送到listview中 这就是我试过的 dataItems: new ObservableArray(), onLoadMoreItemsRequested: function (args) { console.log("---load more item---"); const

我创建了一个socket.io服务器,用于我的nativescript应用程序。它工作正常,但我遇到的问题是,当“chat message”事件被触发时,我想将新消息推送到listview中

这就是我试过的

dataItems: new ObservableArray(),
onLoadMoreItemsRequested: function (args) {
        console.log("---load more item---");
        const that = new WeakRef(this);
        const listView = args.object;
        if (this._sourceDataItems.length > 0) {
          setTimeout(function () {
            that.get().addMoreItemsFromSource(25);
            listView.notifyLoadOnDemandFinished();
            socketIO.on('chat message', function (data) {
              //console.dir(msg);
              var toast = Toast.makeText("New Message");
              toast.show();
              this.dataItems.push(
                {
                    sender: data.sender,
                    message: data.message,
                    date: data.date,
                    type: data.type
                  }
                );
            });
          }, 1500);
          args.returnValue = true;
        } 
      },   
im获取的错误是“无法读取未定义的属性推送”


请告诉我如何修复这个

我使用了这个,它工作正常

viewModel.dataItems.push(
                {
                    sender: data.sender,
                    message: data.message,
                    date: data.date,
                    type: data.type
                  }
                );

我将此作为注释而不是答案发布,因为我不熟悉nativescript,但如果您使用
this.dataItems.push中的
this.dataItems.push,您的代码似乎可以工作,因为
dataItems
没有声明并定义为
this.dataItems
,而仅仅是
dataItems