I am using ejs and nodejs. Trying to post an array to a file. I can visualize and get the array in ejs file so it is ok to show on the page but when I try to save in a file, it return [object Object] in file. But I can see the values in array.
const express = require('express');
const router = express.Router();
const fs = require('fs');
const todos = [];
const file = 'ToDox.txt'
// /admin/add-product => GET
router.get('/', (req, res, next) => {
res.render('index', { pageTitle: 'Add ToDo Page'});
});
// /admin/add-product => POST
router.post('/', (req, res, next) => {
todos.push({ title: req.body.title, description: req.body.description});
res.redirect('/todos');
console.log(todos);
fs.writeFile(file, todos, (err) => {
if (err) console.log(err);
console.log('Successfuly written to the file!');
})
});
exports.routes = router;
exports.todos = todos;