how to import excel file in to mysql using nodejs - mysql

I am trying to import data in excel file to mysql just like row colon using nodejs are there any references i can learn or any module in nodejs that does my work or any sample code

I used Npm packages "xlsx-to-json-lc" and "xls-to-json-lc" to import excel file to json directly without converting to csv. Hope this helps...
var storage = multer.diskStorage({ //multers disk storage settings
destination: function (req, file, cb) {
cb(null, './uploads/')
},
filename: function (req, file, cb) {
var datetimestamp = dateFormat(new Date(), "yyyy~mm~dd h~MM~ss");
cb(null, '`enter code here`templete' + '-' + datetimestamp + '.' +
`enter code here`file.originalname.split('.')[file.originalname.split('.').length - 1])
filename = file.fieldname;
}
});
var upload = multer({ //multer settings
storage: storage,
fileFilter: function (req, file, callback) { //file filter
if (['xls', 'xlsx'].indexOf(file.originalname.split('.')[file.originalname.split('.').length - 1]) === -1) {
return callback(new Error('Wrong extension type'));
}
callback(null, true);
}
}).single('file');
var exceltojson;
upload(req, res, function (err) {
if (err) {
res.json({ error_code: 1, err_desc: err });
return;
}
if (!req.file) {
//res.json({ error_code: 1, err_desc: err });
return;
}
if (req.file.originalname.split('.')[req.file.originalname.split('.').length - 1] === 'xlsx') {
exceltojson = xlsxtojson;
} else {
exceltojson = xlstojson;
}
try {
exceltojson({
input: req.file.path,
output: null, //since we don't need output.json
//lowerCaseHeaders: true
}, function (err, result) {
if (err) {
return res.json({ error_code: 1, err_desc: err, data: null });
}
else {
console.log(result);
}
});
})

Related

TypeError: Cannot read properties of undefined (reading 'filename') in multer

I have a very similar problem with respect to this fellow community contributor. How do i produce multer error message in my postman I followed through the comments made by other users and it was successful! However, when i tried to post a image that is a jpg formatted image( which i managed to do before the editing), it now fails and state that TypeError: Cannot read property 'filename' of undefined.
// multer.js file
successfully setup multer
**please tell me why this error comes on my code and give me a solution**
const multer = require('multer');
const storage = multer.diskStorage({
fileSize: 1024*1024*2,
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
cb(null, file.fieldname + '-' + uniqueSuffix)
}
})
const filter = function (req, file, cb) {
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
cb(null, true);
} else {
cb(new Error('unsupported files'), false)
}
}
var upload = multer({
storage: storage,
limits: {
fileSize: 1024 * 1024 * 5
},
fileFilter : filter
});
module.exports = upload;
//controller.js file
//create function
here's my logic to create a new user
exports.create = (req, res, next) => {
if (!req.body) {
res.status(400).send({ message: "content cannot be empty !!" })
return
}
let data = { name: req.body.name, description: req.body.description, brand_url:
req.body.brand_url, image_file: req.body.file.filename }; getting error here
let sql = "INSERT INTO influencer SET ?";
db.query(sql, data, (err, results) => {
if (err) throw err;
console.log('data inserted succesfully')
res.redirect('/admin');
});
}
//api.js file
//post API
router.post('/api/create', upload.single('image') ,controller.create) //when I am
sending file its throw back error undefined filename
Please make sure you have added enctype="multipart/form-data"
<form action="/api/create" enctype="multipart/form-data" method="post">
I have tested the codes & found the problem.
exports.create = (req, res, next) => {
if (!req.body) {
res.status(400).send({ message: "content cannot be empty !!" })
return
}
let data = {
name: req.body.name,
description: req.body.description,
brand_url: req.body.brand_url,
image_file: req.file.filename
}; // Remove "body", I have tested, it works well.
let sql = "INSERT INTO influencer SET ?";
db.query(sql, data, (err, results) => {
if (err) throw err;
console.log('data inserted succesfully')
res.redirect('/admin');
});
}

req.file.stream is undefined

I'm having troubles with node 16 and ES6. I'm trying to make a upload file controller but i'm stuck with req.file.stream which is undefined
I'm using multer to handle upload files.
The first issue was __dirname undefined that I was able to fix with path and New Url.
The error I got with pipeline
node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "source" argument must be of type function or an instance of Stream, Iterable, or AsyncIterable. Received undefined
my userRoutes.js
import express from "express";
import { signin, signup, logout } from "../Controller/AuthController.js";
import {
getUsers,
getUser,
updateUser,
deleteUser,
follow,
unfollow,
} from "../Controller/UserController.js";
import { upload } from "../Controller/UploadController.js";
import multer from "multer";
const router = express.Router();
// Auth
router.post("/signin", signin);
router.post("/signup", signup);
router.post("/logout", logout);
// users
router.get("/", getUsers);
router.get("/:id", getUser);
router.patch("/:id", updateUser);
router.delete("/:id", deleteUser);
router.patch("/follow/:id", follow);
router.patch("/unfollow/:id", unfollow);
// upload
router.post("/upload", multer().single("file"), upload);
export default router;
And my UploadController.js
import fs from "fs";
import { promisify } from "util";
import stream from "stream";
const pipeline = promisify(stream.pipeline);
// const { uploadErrors } = require("../utils/errors.utils");
import path from "path";
const __dirname = path.dirname(new URL(import.meta.url).pathname);
export const upload = async (req, res) => {
try {
// console.log(req.file);
console.log(__dirname);
if (
!req.file.mimetype == "image/jpg" ||
!req.file.mimetype == "image/png" ||
!req.file.mimetype == "image/jpeg"
)
throw Error("invalid file");
if (req.file.size > 2818128) throw Error("max size");
} catch (err) {
const errors = uploadErrors(err);
return res.status(201).json({ err });
}
const fileName = req.body.name + ".jpg";
await pipeline(
req.file.stream,
fs.createWriteStream(
`${__dirname}/../client/public/uploads/profil/${fileName}`
)
);
try {
await User.findByIdAndUpdate(
req.body.userId,
{ $set: { picture: "./uploads/profil/" + fileName } },
{ new: true, upsert: true, setDefaultsOnInsert: true },
(err, docs) => {
if (!err) return res.send(docs);
else return res.status(500).send({ message: err });
}
);
} catch (err) {
return res.status(500).send({ message: err });
}
};
Multer gives you the file as a Buffer, not a Stream. req.file.stream is not valid property, but req.file.buffer is: https://github.com/expressjs/multer#file-information.
From the look of your code, you're trying to save the file on disk. You can use multer's DiskStorage for that. Create a storage instance and pass it to the multer instance as a configuration:
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, `${__dirname}/../client/public/uploads/profil/`);
},
filename: function (req, file, cb) {
cb(null, req.body.name + '.jpg');
},
});
const upload = multer({ storage });
router.post('/upload', upload.single('file'), upload);
Have a look at this free Request Parsing in Node.js Guide for working with file uploads in Node.js.
if you want to use req.file.stream, you will need to install this version of multer:
npm install --save multer#^2.0.0-rc.1
and your code will work perfectly, just change your req.file.mimetype to req.file.detectedMimeType !!

Error In Multer When Trying To Upload A Image

Currently new to multer, so here's my problem:
I kept getting the MulterError: Unexpected field as the output when i try to POST the jpg image into mysql. Can anyone see whats the bug in my code? Currently im trying to make a error message when an image of more than 1MB is uploaded.
var multer = require('multer');
var path = require('path');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + new Date().toISOString() + '-' + path.extname(file.originalname));
}
});
var upload = multer({storage: storage});
// Endpoint 13 (EXTRA FEATURE 1)
const fileFilter = (req, file, callback) => {
if(file.mimetype === 'image/jpg') {
callback(null,true);
}
callback(null,false);
}
var uploadFromDesktop = multer({
storage:storage,
limits: {
fieldSize: 1024 * 1024
},
fileFilter: fileFilter
});
app.post("/upload/", upload.single('productImage'), (req, res, next) => {
uploadFromDesktop(req,res),(error) => {
console.log(error);
if (error) {
if (error.code === 'LIMIT_FILE_SIZE') {
} else {
res.status(500).send({ 'status': status, 'error':error})
}
} else {
var temp = req.file.path;
var file = uploadDir + req.file.originalname;
var source = fs.createReadStream(temp);
var destination = fs.createWriteStream(file);
source.pipe(destination);
fs.unlink(temp);
source.on('end',function () {
var result = {
'status': 'success',
'file': file
}
fs.chmod(file,0777);
res.send(result);
})
source.on('error', function (error) {
var result = {
'status': 'Fail',
'error': error
};
res.send(result);
})
}
}
})
Try with fileSize:
limits: {
fileSize: 2 * 1024 * 1024 // 2 MB}

Getting file from hash in js-ipfs

I'm running the script below and using an ipfs node to upload and get a file using its hash, but the ipfs cat function only returns the path of the file from the hash, not the content.
const node = new Ipfs()
node.once('ready', () => console.log('IPFS node is ready'))
$("#saveIt").click(function(){
var toStore = document.getElementById('fileInput').value
node.files.add(new node.types.Buffer.from(toStore), (err, res) => {
if (err || !res) {
return console.error('ipfs add error', err, res)
}
res.forEach((file) => {
if (file && file.hash) {
var newVar = file.hash
var newVar1 = newVar.slice(0, 23)
var leng = newVar.length
var newVar2 = newVar.slice(24, leng)
console.log(newVar1 + ' ' + newVar2)
mediachain.setUserFile($("#passwordSetter").val(), newVar1, newVar2)
node.files.cat(file.hash, (err, data) => {
if (err) {
return console.error('ipfs cat error', err)
}
document.getElementById('fileDisplayArea').innerText = data
})
} else {
console.error("Error: invalid file")
}
})
})
})
Does anyone have experience with js-ipfs and can help me out?
I had a similar problem with ipfs.
if you have hash already you can get the content this way.
const validCID = 'QmQFPQ5f94byxs7zvHMLJcx5WzThRhN4MfAF4ZisSXofKC'
ipfs.files.get(validCID, function (err, files) {
files.forEach((file) => {
console.log(file.path)
console.log("File content >> ",file.content.toString('utf8'))
})
})

How to upload file using multer and store details in database in mean rest app and retrieve it from angular.js

I am uploading file through my rest mean application, but i am not able to retrieve it back from backend . how do i retrieve it,
Here is upload code
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback) {
callback(null, mongoose.Types.ObjectId() + '-' + file.originalname);
}
});
var upload = multer({ storage : storage }).array('userPhoto',10);
upload(req,res,function(err) {
console.log(req.files);
var images =[];
for(var i=0; i<req.files.length; i++){
images[i]=req.files[i].path;
}
var newalbum = new albummodel({
image:images
});
newalbum.save(function(err, albm) {
if(err) {
res.json({success: false, msg: 'can't store.'});
} else {
console.log(albm);
}
});
if(err) {
return res.end("Error uploading file.");
}
res.end("File is uploaded");
});
Here is my retrieve code
albummodel.findOne({_id:req.params.id},function(err, docs){
res.json(docs);
})
This whole discussion help you. Please refer it.
https://github.com/Automattic/mongoose/issues/3079