Javascript 获取丢失的错误处理程序套接字消息,无法读取属性';timerID';使用Node.js和Socket.io未定义的

Javascript 获取丢失的错误处理程序套接字消息,无法读取属性';timerID';使用Node.js和Socket.io未定义的,javascript,node.js,sockets,Javascript,Node.js,Sockets,我收到以下错误,使用Node.js和socket.io运行服务器 错误 客户端HTMLPage2.HTML $(文档).ready(函数(){ 函数更改图标(父级、从、到){ var target=$('.ui btn text',parent).next(); target.removeClass(from).addClass(to); } 函数更改计数(索引、计数){ $('li[data index='+index+'].ul li count').html(count); } var

我收到以下错误,使用Node.js和socket.io运行服务器

错误

客户端HTMLPage2.HTML


$(文档).ready(函数(){
函数更改图标(父级、从、到){
var target=$('.ui btn text',parent).next();
target.removeClass(from).addClass(to);
}
函数更改计数(索引、计数){
$('li[data index='+index+'].ul li count').html(count);
}
var socket=io.connect();
socket.on('count',函数(数据){
变更计数(data.index,data.count);
});
$('.product>a[数据图标])。单击(函数(){
if($(this.attr('toggle')!='off'){
var index=$(this.attr('data-index');
socket.emit('cart',编号(索引));
changeIcon(这是“ui图标检查”、“ui图标返回”);
$(this.attr('toggle','off');
}否则{
var index=$(this.attr('data-index');
如果(确认)물건을 구매하겠습니까?')){
socket.emit('buy',Number(index));
$(this.parent().remove();
$('#listview')。listview('refresh');
}否则{
socket.emit('return',Number(index));
changeIcon(此“ui图标返回”、“ui-icon-check”);
}
$(this.attr('toggle','on');
}
});
});
商场
  • 产品
我在此行遇到错误。“clearTimeout(cart[index].timerID);”


请帮助我解决此错误。

当用户希望直接购买商品而不将其放在购物车上时,会触发此错误。在这种情况下,
cart
事件将永远不会触发,因此,
cart[index]
将永远不存在

socket.on('buy', function(index){

    if(cart[index]){

        clearTimeout(cart[index].timerID);


        delete cart[index];
    }

    io.sockets.emit('count', {
        index: index,
        count: products[index].count
    });
});
此外,您还应该像这样处理错误事件:

socket.on('error',function(er){
    console.log(er);
});

当用户希望直接购买商品而不将其放在购物车上时会触发此错误。在这种情况下,
cart
事件将永远不会被触发,因此,
cart[index]
将永远不存在

socket.on('buy', function(index){

    if(cart[index]){

        clearTimeout(cart[index].timerID);


        delete cart[index];
    }

    io.sockets.emit('count', {
        index: index,
        count: products[index].count
    });
});
此外,您还应该像这样处理错误事件:

socket.on('error',function(er){
    console.log(er);
});
socket.on('error',function(er){
    console.log(er);
});