Запись в файл heroku
Имею задеплоеный на хероку проект. Проблема в этой строке, как я понял.
fs.writeFileSync('data/contacts.json', JSON.stringify(newData));
Код:
console.log(newData);
fs.writeFileSync('data/contacts.json', JSON.stringify(newData));
console.log(require('./data/contacts.json'));
Получаю
[
{ id: '78', name: 'wqe', phone: '335' },
{ id: '264', name: 'qц', phone: '123123' },
{ id: '546', name: 'www', phone: '123131' }
]
[
{ id: '264', name: 'qц', phone: '123123' },
{ id: '546', name: 'www', phone: '123131' }
]
Почему так?
Хотя в похожих местах
contacts.splice(index, 1);
console.log(contacts);
fs.writeFileSync('data/contacts.json', JSON.stringify(contacts));
console.log(require('./data/contacts.json'));
Вывод
[
{ id: '264', name: 'qц', phone: '123123' },
{ id: '546', name: 'www', phone: '123131' }
]
[
{ id: '264', name: 'qц', phone: '123123' },
{ id: '546', name: 'www', phone: '123131' }
]
И ещё
for (let i = 0; i < contacts.length; i++)
if (contacts[i].id === newPerson.id)
contacts[i] = newPerson;
fs.writeFileSync('data/contacts.json', JSON.stringify(contacts));
console.log(require('./data/contacts.json'));
Верный вывод
[
{ id: '264', name: 'qц', phone: '123123' },
{ id: '546', name: 'wwwwwww', phone: '123131' }
]