NodeJS: Write to JSON file removes existing data - node.js

I am working on this method:
router.use(bodyparser.json());
router.post('/', (req, res) => {
let rooms;
//let roomID = methoden.idErstellen(rooms, filename);
try {
rooms = {
//id: roomID,
id:req.body.id,
name: req.body.name
};
//createRessource(filename)
let data = JSON.stringify(rooms, null, 2);
fs.writeFile(filename, data, (err)=> {
if(err) throw err;
console.log('Data written to file')
});
//res.status(201).send(rooms);
} catch (error) {
res.sendStatus(500)
}
refresh();
});
The method adds a new data set to the JSON file, but removes all the existing ones. I can't figure out what the problem with this is

Try using fs.appendFile
fs.appendFile(filename, data, function (err) {
if (err) throw err;
console.log('Data written to file successfully');
});

Related

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'unlink' of undefined at Grid.remove

I'm trying to delete a file by its id using gridfs but I get this error when calling the delete API.
Controller :
let gfs;
connect.once("open", () => {
gfs = Grid(connect.db, mongoose.mongo);
gfs.collection("uploads");
});
exports.deleteFile = (req, res) => {
try {
gfs.remove(
{ _id: req.params.id, root: "uploads" },
(err, gridStore) => {
if (err) {
return res.status(404).send({ message: err });
} else {
return res.send({ message: "File deleted successfuly" });
}
}
);
} catch (error) {
return res.status(500).send({
message: error.message,
});
}
};
exports.deleteFileByFilename = async (req, res, next) => {
const file = await gfs.files.findOne({ filename: req.params.filename });
const gsfb = new mongoose.mongo.GridFSBucket(conn.db, { bucketName: 'uploads' });
gsfb.delete(file._id, function (err, gridStore) {
if (err) return next(err);
res.status(200).end();
});
};
// #route DELETE /files/:filename
// #desc Delete file
app.delete('/files/:filename', async (req, res) => {
const file = await gfs.files.findOne({ filename: req.params.filename });
const gsfb = new mongoose.mongo.GridFSBucket(conn.db, { bucketName: 'uploads' });
gsfb.delete(file._id, function (err, gridStore) {
if (err) {
res.status(404).send('no file found')
}
res.status(200).send('deleted successfully')
});
});
On client side:
const delImage = async (fileName) => {
await axios.delete(`http://localhost:5000/files/${fileName}`);
console.log('fileDeleted');
getData(); // reminder to REFETCH the data so that database with deleted file is refreshed
}
I also made a video on this - full file uploads, multiupload, display and delete using MERN stack and NPM Multer thing, in here: https://youtu.be/4WT5nvfXcbs
Docs for the video with full code: https://docs.google.com/document/d/1MxvNNc9WdJT54TpanpFBT6o7WE_7hPAmDhdRNWE8A9k/edit?usp=sharing

How to create a document on memory nodejs, because I have variable with csv data, but i don't know how to send the file with out save it

How to create a docuement on memory nodejs, because I have variable with csv data, but i don't know how to send the file without saving it.
fs.readFile('./test-data.json', 'utf-8', (err, fileContent) => {
if (err) {
console.log(err); // Do something to handle the error or just throw it
throw new Error(err);
}
const csvData = csvjson.toCSV(fileContent, {
headers: 'key'
});
fs.writeFile('./test-data.csv', csvData, (err) => {
if (err) {
console.log(err); // Do something to handle the error or just throw it
throw new Error(err);
}
console.log('Success!');
bot.sendDocument(chatId, './test-data.csv');
});
});

Error-handling in asynch functions

I want to throw an error into the console if the following function doesn't work for any reason. It's getting some data from a website, writing them into a mongoDB.
If for example the insert into mongoDB or the scraping fails I want to get an error message in the console. I have no idea how to archive proper error-handling with nodejs (0 clue about promises and stuff).
artikel.getData(async () => { for (let i = 0; i < arrayOfArticles.length; i++){
await scrape(i).then((price) => {
console.log('Data ' + arrayOfArticles[i] + ': ' + received);
//Connect to DB
MongoClient.connect(url, {useNewUrlParser: true}, function(err, db) {
if (err) throw err;
let dbo = db.db("testDB");
let insertPart = {
name: arrayOfArticles[i],
site: dealer,
price: price
};
dbo.collection("testcollection").insertOne(insertPart, function(err, res) {
if (err) throw err;
console.log(divide);
console.log("Document inserted");
console.log(divide);
db.close();
});
});
});
}
});
You should not write DB connection code inside the loop or inside API. It should be in some config file.
You don't need to write .then with await, use try-catch for error handling.
artikel.getData(() => {
MongoClient.connect(url, {
useNewUrlParser: true
}, async function (err, db) {
for (let i = 0; i < arrayOfArticles.length; i++) {
try {
const price = await scrape(i);
//Connect to DB
if (err) throw err;
let dbo = db.db("testDB");
let insertPart = {
name: arrayOfArticles[i],
site: dealer,
price: price
};
dbo.collection("testcollection").insertOne(insertPart, function (err, res) {
if (err) {
console.log(err);
throw err
};
console.log(divide);
console.log("Document inserted");
console.log(divide);
});
} catch (error) {
console.log(error);
}
}
db.close();
});
});

Why my nodejs requests are slow?

Im new in nodejs, and Im trying to learn by creating an app that has a list of users, that I can add and remove those users. Im using angularjs in frontend to send request to nodejs and after that to mongodb. The problem is that, if I click a lot of times in the button "adduser" a lot of times, my app goes slow.
To interact to mongodb I use:
app.get('/users',function (req, res) {
mongoose.model('Usuario').find(function (err, list) {
res.send(list);
});
});
app.post('/addusuario', function (req,res) {
var usuario = new Usuario(req.body);
usuario.save(function (err) {
if (err) {
console.log(err);
} else {
console.log('Usuario salvo com sucesso');
}
}); });
app.delete('/delusuario/:id', function (req, res) {
var id = req.params.id;
mongoose.model('Usuario').findByIdAndRemove(id , function(err) {
if(err) {
console.log(err);
} else {
console.log('Usuario removido com sucesso!');
}
});
});
Im my angularapp:
app.controller('AppCtrl', function($scope, $http, Data) {
function reload() {
Data.get('users').then(function(data){
$scope.usuarios = data;
console.log(data);
});
};
$scope.addUsuario = function(usuario) {
Data.post('/addusuario', usuario);
reload();
};
$scope.deletarUsuario = function(id) {
Data.delete("/delusuario/"+id).then(function(result) {
});
reload();
};
reload();
});
I dont know why it is becaming slow after I click to add user more than 3 times..
What I see in your code that you are not sending an response back to the user, you should do something after insert or delete in the database. res.end();
You should rewrite your code in the following way:
app.get('/users',function (req, res) {
mongoose.model('Usuario').find(function (err, list) {
res.send(list);
});
});
app.post('/addusuario', function (req,res) {
var usuario = new Usuario(req.body);
usuario.save(function (err) {
if (err) {
console.log(err);
res.json({err: err});
} else {
res.json({ok: true});
console.log('Usuario salvo com sucesso');
}
}); });
app.delete('/delusuario/:id', function (req, res) {
var id = req.params.id;
mongoose.model('Usuario').findByIdAndRemove(id , function(err) {
if(err) {
console.log(err);
res.json({err: err});
} else {
res.json({ok: true});
console.log('Usuario removido com sucesso!');
}
});
});
You block the stack by not returning the response to the client. And this is most probably the cause of your slow request.

How to send response in Node Express with file and data

module.exports.validateUser = function (req, res) {
User.find({ 'username': req.body.username, 'password': req.body.password }, function (err, result) {
if (result.length) {
var gridfs = req.app.get('gridfs');
var readstream = gridfs.createReadStream({
_id: result[0]._doc.picture
});
req.on('error', function (err) {
res.send(500, err);
});
readstream.on('error', function (err) {
res.send(500, err);
});
// Just sends the file
//readstream.pipe(res);
res.send('This is incedible');
} else {
res.send(false);
}
});
};
Just after the user is validated I am getting the file associated with it. Further I want to send some data along with the fine in response. I've used multer and gridfs-stream. Is this achievable. If not, then does something looks fishy in this approach?

Resources