Uncaught ReferenceError: data is not defined
at app.js:14:46
at Array.forEach (<anonymous>)
at Object.success (app.js:12:26)
at u (jquery.min.js:2:27457)
at Object.fireWith [as resolveWith] (jquery.min.js:2:28202)
at k (jquery.min.js:2:77651)
at XMLHttpRequest.<anonymous> (jquery.min.js:2:79907)
app.js:70 Invalid Date
Вот код:
$(document).ready(()=>{
check()
const socket = io()
const cred = JSON.parse(localStorage.getItem("userData"))
$.ajax({
url: "/getmessage",
type: "POST",
success: function(msg) {
if(msg.status == "err"){
new Notification('Ошибка', {body: msg.msg})
}else{
msg.data.forEach(el => {
// Форматирование времени
let msgDate = formatDate(data.time_mark)
let currentDate = formatDate(new Date())
var x = new Date(currentDate); // 1 Дата
var y = new Date(msgDate); // 2 Дата
if(Date.parse(x) == Date.parse(y)){
console.log("=")
var Strtime = "Сегодня, в "+`${new Date(data.time_mark).getHours()}:${new Date(data.time_mark).getMinutes()}`
} else {
var date = new Date(data.time_mark); // 2009-11-10
var month = date.toLocaleString('default', { month: 'long' });
console.log(new Date(data.time_mark))
var Strtime = `${new Date(data.time_mark).getDay()} ${month} ${new Date(data.time_mark).getFullYear()} , в ${new Date(data.time_mark).getHours()}:${new Date(data.time_mark).getMinutes()}`
}
$("#msg-container").append(`
<div class="msg-box" id='${data.id}'>
<p class="author">${data.nick}</p>
<p class="msg-text">${data.content}</p>
<span class="time">${Strtime}</span>
</div>
`);
if(data.nick != cred.nick){
$(`#${data.id}`).addClass("not-my")
}
});
$("#all_mess").scrollTop($("#msg-container").height())
}
}
});
socket.emit('hello', ({nick: cred.nick}))
$("#messForm").submit((e)=>{
e.preventDefault();
let msg = $("#message").val()
if(!msg){
}else{
socket.emit('sendMess', {token: localStorage.getItem('token'), msg: $("#message").val()});
$("#message").val('');
}
})
socket.on('addMess', function (data) {
// Форматирование времени
let msgDate = formatDate(data.time_mark)
let currentDate = formatDate(new Date())
var x = new Date(currentDate); // 1 Дата
var y = new Date(msgDate); // 2 Дата
if (Date.parse(x) == Date.parse(y)) {
console.log("=")
var Strtime = "Сегодня, в " + `${new Date(data.time_mark).getHours()}:${new Date(data.time_mark).getMinutes()}`
} else {
var date = new Date(data.time_mark); // 2009-11-10
var month = date.toLocaleString('default', { month: 'long' });
console.log(new Date(data.time_mark))
var Strtime = `${new Date(data.time_mark).getDay()} ${month} ${new Date(data.time_mark).getFullYear()} , в ${new Date(data.time_mark).getHours()}:${new Date(data.time_mark).getMinutes()}`
$("#msg-container").append(`
<div class="msg-box" id='${data.id}'>
<p class="author">${data.author}</p>
<p class="msg-text">${data.msg}</p>
<span class="time">${Strtime}</span>
</div>
`);
if (data.author != cred.nick) {
$(`#${data.id}`).addClass("not-my")
}
$("#all_mess").scrollTop($("#msg-container").height())
}
});
socket.on('toLogin', function(data){
window.location.href = '/auth'
})
socket.on('helloMess', (str)=>{
$("#msg-container").append(`<span class="hello">${str.mess}</span>`)
$("#all_mess").scrollTop($("#msg-container").height())
//setInterval(hideEl, 3000)
})
});
function hideEl(){
$('.hello').hide()
}
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}