I'm now stuck with my project using NodeJS and Busboy. I'm trying to retrieve all the variables from my router and send it to controllers.
app.js (routes)
router.post('/product', function (req, res) {
var fields = {};
req.busboy.on('field', function(fieldname, val) {
fields[fieldname] = val;
});
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
fields[filename] = file;
});
req.pipe(req.busboy);
productController.addNewProduct(fields, function (data) {
res.json(data);
});
});
ProductController.js
addNewProduct: function (params, callback) {
productLogic.addNewProduct(params, function (data) {
callback(data);
});
},
ProductLogic.js
addNewProduct: function (params, callback) {
Product.findOne({ name: params.name }, function (err, product) {
if(err) callback({ status: false, message: err });
if(product)
callback({ status: false, message: 'Produk sudah ada.' });
var newProduct = new Product({
name: params.name,
category_id: params.category_id,
description: params.description,
is_active: true
});
newProduct.save(function (err, result) {
if(err) callback({ status: false, message: err });
callback({ status: true, message: result });
});
});
},
My goal is to process the data all at once. And now I'm not sure if I can achieve it with busboy.
Please help me on this.
Bunch of thanks in advance