I have in a project the possibility to add several items at once. And I would like to be able to add several items using a csv.
To have more context (the schema) :
{
"_id": {"$oid": "xxxxxxxxxxxxxxxx"},
"nameCourse": "Name",
"description": "lorem ipsum",
"user": [{
"_id": {"$oid": "xxxxxxxxxxxxxxxx"},
"name": "John Doe",
"gender": "M",
"city": "New York",
"email": "[email protected]"
},
{
"_id": {"$oid": "xxxxxxxxxxxxxxxx"},
"name": "Jane Doe",
"gender": "F",
"city": "Tokyo",
"email": "[email protected]"
},
{
"_id": {"$oid": "xxxxxxxxxxxxxxxx"},
"name": "Jake Doe",
"gender": "M",
"city": "Paris",
"email": "[email protected]"
}],
}
(I'm using NodeJs/ MongoDb (with Mongoose) / Express / EJS )
It has a form with an input:file and a button.
<form method="POST" id="add_multiple_user">
<input type="file" name="user">
<button type="submit">Send</button>
</form>
<script>
$("#add_multiple_user").submit(function (event) {
event.preventDefault();
let idcourse = document.getElementById("id_course");
let unindexed_array = $(this).serializeArray();
let data = {};
$.map(unindexed_array, function (n, i) {
data[n["name"]] = n["value"];
});
let addAdresseMultiple = {
url: `/api/course/add-multiple-user/${idcourse.innerText}`,
method: "PUT",
data: data,
};
$.ajax(addAdresseMultiple).done(function (response) {
location.reload();
});
});</script>
Here my controller :
exports.addMultipleUser = async (req, res) => {
const { name, gender, city, email } = req.body;
try {
await UserModel.findOneAndUpdate(
{ _id: req.params.id },
{
$addToSet: {
user: {
name: name,
gender: gender,
city: city,
email: email,
},
},
},
(err, docs) => {
if (!err) return res.send(docs);
if (err) return res.status(400).send({ message: err });
}
);
} catch (err) {
return res.status(400).json({ message: err });
}
};
I saw that there some package (like fast csv) but i didn't understand at all how it can help me.
CodePudding user response:
I am not going to post any answer for you but here is a quick summary of what you might need to breakthrough.
- Understand multer
- Convert CSV to JSON
- Create multiple items
CodePudding user response:
Thanks to @Koodies for directing me on the right path.
Here's the answer :
in the controller :
exports.addMultipleUser = async (req, res) => {
try {
await csv({delimiter:";"})
.fromFile(req.file.path)
.then((jsonObj)=>{
console.log(jsonObj);
UserModel.findOneAndUpdate(
{ _id: req.params.id },
{ $addToSet: { sites: jsonObj } },
(err, docs) => {
if (!err) return res.send(docs);
if (err) return res.status(500).send({ message: err });
}
);
fs.unlink(req.file.path, (err) => {if(err){console.log(err); return;}})
});
} catch (err) {
return res.status(500).json({ message: err });
}
};
my form & jquery:
<form method="POST" id="add_multiple_user" >
<input type="file" name="user" id="myFileUser">
<button type="submit">Send</button>
</form>
<script>$("#myFileUser").change(function (e) {
let file = e.target.files[0];
let formData = new FormData();
formData.append("myFileUser", file);
$("#add_multiple_user").submit(function (event) {
event.preventDefault();
let idcourse = document.getElementById("id_course");
$.ajax({
url: `/api/course/add-multiple-user/${idcourse.innerText}`,
type: "PUT",
data: formData,
processData: false,
contentType: false,
success: function (res) {
console.log(res);
},
crossDomain: true,
}).done(function(res) {location.reload()});
});
});</script>