stringify丢失属性-记录并保存所有javascript事件
我正在尝试记录并保存用户会话中发生的所有事件 为了做到这一点,我有一个数组stringify丢失属性-记录并保存所有javascript事件,javascript,json,mongodb,mongoose,Javascript,Json,Mongodb,Mongoose,我正在尝试记录并保存用户会话中发生的所有事件 为了做到这一点,我有一个数组 var events = [] 每个事件都有一个监听器 window.onload = function() { window.addEventListener("abort", handleEvent); window.addEventListener("blur", handleEvent); [...] } 以及发送事件: function sendEvents() { axio
var events = []
每个事件都有一个监听器
window.onload = function() {
window.addEventListener("abort", handleEvent);
window.addEventListener("blur", handleEvent);
[...]
}
以及发送事件:
function sendEvents() {
axios.post('/api/logger', JSON.stringify(events));
}
问题是每个sendEvents都发送一个事件数组,但每个事件只有两个属性:
[{"isTrusted":true,"date":"2019-02-19T14:59:42.474Z"},{"isTrusted":true,"date":"2019-02-19T14:59:42.485Z"}
但它应该有更多的属性,例如:
*altKey: false
bubbles: true
button: 0
buttons: 0
cancelBubble: false
cancelable: true
clientX: 662
clientY: 273
composed: true
ctrlKey: false
currentTarget: null
date: Tue Feb 19 2019 12:08:38 GMT-0300 (hora estándar de Argentina) {}
defaultPrevented: false
detail: 0
eventPhase: 0
fromElement: null
isTrusted: true
layerX: 662
layerY: 273
metaKey: false
movementX: 0
movementY: 0
offsetX: 662
offsetY: 273
pageX: 662
pageY: 273
path: (7) [header.App-header, div.App, div#root, body, html, document, Window]
relatedTarget: null
returnValue: true
screenX: 662
screenY: 400
shiftKey: false
sourceCapabilities: InputDeviceCapabilities {firesTouchEvents: false}
srcElement: header.App-header
target: header.App-header
timeStamp: 1605.000000000473
toElement: header.App-header
type: "mouseover"
which: 1
x: 662
y: 273*
即使我没有字符串化数组,在后端,对象也只有两个相同的属性。我正在使用nodejs+mongodb
如何保存整个事件对象及其所有属性?可能有几个原因。最有可能的是这些属性不可枚举,这导致它们不包含在JSON中。属性定义为可通过枚举。见: 我建议您明确声明要发布的属性,因为您可能并不需要所有属性:
axios.post('/api/logger', JSON.stringify(events.map(event=>{
return {
altKey: event.altKey,
bubbles: event.bubbles,
// etc...
}
}));
handleEvent做什么?const handleEvent=e=>{e.date=new date;events.push;我的意思是,它只需添加当前日期并将对象推送到数组中,他实际上很幸运拥有这个日期,我刚刚测试过,只得到了{isTrusted:true}。他还应该对TypeError保持警惕:循环对象值错误非常有效。这就是问题所在,这些属性不可枚举,因此json.stringify会忽略它。谢谢!