Post Multiple Objects Inside Express Route - javascript

I would like to post multiple objects to my mongo database inside of an express route. Currently, everything is working fine when I do it as a single object (ie ONE casino), please see below, but instead of doing this a million times over, can someone help me do it as one giant data dump so I can post ALL my casinos?
Here is my route that works fine for posting a single object:
router.post('/post', async (req, res) => {
console.log(req.body);
const casinoD = new Casino({
casino: req.body.casino,
table_and_other: req.body.table_and_other,
poker: req.body.poker,
slot_machines: req.body.slot_machines,
total_gaming_win: req.body.total_gaming_win,
year: req.body.year,
month: req.body.month,
combined_date: req.body.combined_date
})
try {
const newCasino = await casinoD.save()
res.status(201).json(newCasino)
} catch (err) {
res.status(400).json({ message: err.message})
}
})
I also understand mongoimport is a better way to do this - however that had its own issues in of itself.
Thanks

Like #JDunken said, you can iterate over the POST body as an array and insert in bulk. You'll want to use
insertMany for speed. To insert millions of records, you will probably want to put a sane limit on the number of records per request, and send API requests in batches. Validation is optional, as Mongoose will run validation according to the schema. It depends on how you want to handle validation errors. Make sure to read up on the ordered and rawResult options for that as well.
router.post('/post', async (req, res) => {
// you should sanity check that req.body is an array first, depending on how robust you want error handling to be
const casinos = req.body.filter(input => isValid(input));
try {
const insertedCasinos = await CasinoModel.insertMany(casinos, { ordered: false });
res.status(201).json(insertedCasinos)
} catch (err) {
res.status(400).json({ message: err.message})
}
})
const isValid(input) {
let valid = true;
// implement input validation
return valid;
}

Related

get route showing null in response

I'm using TypeScript for making a GET request to get all members whose isCore is true. I've made several entries in the SQL database but it is showing null in res.json. Is the condition syntax is correct?
code:
router.get('/coreMem', async(req, res)=>{
try {
const core_member_details = await Member.findAll({
where:{
isCore: true
}
})
res.status(200).json(core_member_details);
}
catch (err) {
logger.error(`${err}`);
res.status(500).send('Internal Server/Database Error!');
}
});
I think that it, make sure that the Member model is correctly defined and that it is able to connect to the correct table in the database.
if not, a tip for you which is good debugging method:
To return all the records from the members table, you can use the findAll method without any conditions, like this:
router.get('/coreMem', async(req, res)=>{
try {
const all_member_details = await Member.findAll();
res.status(200).json(all_member_details);
},
catch (err) {
logger.error(${err});
res.status(500).send('Internal Server/Database Error!');
}
});

Error while deleting a value of element in mongoDB array using filter function?

I tried to find the solutions over here but unable to get success while using $pull as the array values I have does not contain `mongo_id'.
So the scenario is that , I am trying to delete the specific comment of the particular user which I am passing through query params. M
My mongo data looks like this:
Now I am making API Delete request like this : http://localhost:8000/api/articles/learn-react/delete-comment?q=1 on my localhost .
ANd finally my code looks like this:
import express from "express";
import bodyParser from "body-parser";
import { MongoClient } from "MongoDB";
const withDB = async (operations, res) => {
try {
const client = await MongoClient.connect(
"mongodb://localhost:27017",
{ useNewUrlParser: true },
{ useUnifiedTopology: true }
);
const db = client.db("my-blog");
await operations(db);
client.close();
} catch (error) {
res.status(500).json({ message: "Error connecting to db", error });
}
};
app.delete("/api/articles/:name/delete-comment", (req, res) => {
const articleName = req.params.name;
const commentIndex = req.query.q;
withDB(async(db) => {
try{
const articleInfo = await db.collection('articles').findOne({name:articleName});
let articleAllComment = articleInfo.comments;
console.log("before =",articleAllComment)
const commentToBeDeleted = articleInfo.comments[commentIndex];
//console.log(commentToBeDeleted)
// articleAllComment.update({
// $pull: { 'comments':{username: commentToBeDeleted.username }}
// });
articleAllComment = articleAllComment.filter( (item) => item != commentToBeDeleted );
await articleAllComment.save();
console.log("after - ",articleAllComment);
//yaha per index chahiye per kaise milega pta nhi?
//articleInfo.comments = gives artcle comment
res.status(200).send(articleAllComment);
}
catch(err)
{
res.status(500).send("Error occurred")
}
},res);
});
I have used the filter function but it is not showing any error in terminal but also getting 500 status at postman.
Unable to figure out the error?
I believe you'll find a good answer here:
https://stackoverflow.com/a/4588909/9951599
Something to consider...
You can use MongoDB's built-in projection methods to simplify your code.
https://docs.mongodb.com/manual/reference/operator/projection/positional/#mongodb-projection-proj.-
By assigning a "unique ID" to each of your comments, you can find/modify the comment quickly using an update command instead of pulling out the comment by order in the array. This is more efficient, and much simpler. Plus, multiple read/writes at once won't interfere with this logic during busy times, ensuring that you're always deleting the right comment.
Solution #1: The recommended way, with atomic operators
Here is how you can let MongoDB pull it for you if you give each of your comments an ID.
await db.collection('articles').updateOne({ name:articleName },
{
$pull:{ "comments.id":commentID }
});
// Or
await db.collection('articles').updateOne({ name:articleName, "comments.id":commentID },
{
$unset:{ "comments.$":0 }
});
Solution #2 - Not recommended
Alternatively, you could remove it by index:
// I'm using "3" here staticly, put the index of your comment there instead.
db.collection('articles').updateOne({ name:articleName }, {
$unset : { "comments.3":0 }
})
I do not know why your filter is erroring, but I would recommend bypassing the filter altogether and try to utilize MongoDB's atomic system for you.

MongoClient's findOne() never resolves on Electron, even when collection is populated

I'm working on making a web app with Electron and I successfully connected to a Mongo DB Atlas database and I'm able to send information to it. However, I seem to be unable to retrieve it. The first snippet of code that I included is how I connected to the database.
MongoClient.connect(URI, (err, client) => {
if (err){
console.log("Something unexpected happened connecting to MongoDB Atlas...");
}
console.log("Connected to MongoDB Atlas...");
currentDatabase = client.db('JukeBox-Jam-DB'); /* currentDatabase contains a Db */
});
Then, this second snippet is how I've been writing to the database, which seems to work perfectly fine.
ipc.on('addUserToDatabase', (event, currentUser) => {
const myOptions = {
type: 'info',
buttons: ['Continue'],
defaultId: 0,
title: 'Success',
message: 'Your account has been created.'
};
dialog.showMessageBox(mainWindow, myOptions);
currentCollection = currentDatabase.collection('UsersInformation');
currentCollection.insertOne(currentUser);
});
Lastly, this is the code that I've been trying to use to retrieve information from the database. I don't see where I could be making a mistake so that it is not working for retrieving, but yes for writing. From my understanding findOne() when passed without parameters should simply return a Promise that resolves to the first entry that matches the query that is passed to it. If a query is not provided then it will resolve to the item that was put in the database first. If there's no entry that matches the query, then it should resolve to null. Any ideas why this isn't working?
ipc.on('checkUsernameRegistration', (event) => {
currentCollection = currentDatabase.collection('UsersInformation');
let myDocument = currentCollection.findOne(); /* I don't understand why this isn't working! */
console.log(myDocument); /* This prints Promise { <pending> } */
if (myDocument !== null){ /* If myDocument is not null, that means that that there is already someone with that username in the DB. */
}
});
Thanks to everyone that is attempting to help me! I've been stuck in this for several hours now.
Try using async/await :
ipc.on('checkUsernameRegistration', async (event) => {
currentCollection = currentDatabase.collection('UsersInformation');
let myDocument = await currentCollection.findOne({ _id: value });
if (myDocument !== null){
console.log(myDocument);
}
});
or, you need to pass a callback, like this:
currentCollection.findOne({ country: 'Croatia' }, function (err, doc) {
if (err) console.error(err);
console.log(doc);
});
This happens because queries are not promises. Actually, I recommend you to study the difference between async and sync code in node.js. Just to understand where the callback should be passed to function, and where you can simply write await Model.method({ options }).

Editing a mongoDB item not updating but not receiving an error

I am desperate for some help with an issue I am having updating an item in a mongoDB database. I am creating a simple note application using nodeJS and I have set it up to add notes and remove notes to and from the database with no issues.
When I try to edit notes however, the update does not persist to the database. I dont get an error back and when I console.log the result it displays the correct data but just wont seem to push it to the data base. I have been using mongoose and findByIdAndUpdate. Here is the JavaScript code for my update route
app.put("/:id", (req, res) => {
Note.findByIdAndUpdate(req.params.id, req.body.note, (err, updatedNote) => {
if(err) {
console.log(err);
} else {
res.redirect(`/${req.params.id}`);
console.log(req.params.id);
console.log(req.body.note);
console.log("note updated");
}
});
});
As I mentioned, I dont get any error and the console.logs return the correct id and updated content. All my routes take me to the place so I don't believe there is an issue with them. Any help would be greatly appreciated. I can also provide more code if needed
Thanks in advance
Will
When updating in MongoDB, you have to reference the field you are trying to update. Your code should look like this
app.put("/:id", (req, res) => {
Note.findByIdAndUpdate(req.params.id, {text: req.body.note}, (err, updatedNote) => {
if(err) {
console.log(err);
} else {
res.redirect(`/${req.params.id}`);
console.log(req.params.id);
console.log(req.body.note);
console.log("note updated");
}
});
});
PS: I am hoping the field in the DB model is called text
To make the code look sexier, you can switch it to Async/Await
app.put("/:id", async (req, res) => {
let updatedNote = await Note.findByIdAndUpdate(req.params.id, {text:
req.body.note}, {new: true}).catch(err => console.log(err))
res.redirect(`/${req.params.id}`);
console.log(req.params.id);
console.log(req.body.note);
console.log("note updated");
});
That updatedNote is the same as before the update is expected behavior – see the mongooe docs:
By default, findOneAndUpdate() returns the document as it was before
update was applied. If you set new: true, findOneAndUpdate() will
instead give you the object after update was applied.
This does not mean the update did not take place.

Twilio SMS message keeps resending the same message

So I'm trying to send notifications to users phones with Twilio, however, the same message is sent three times. I do have three phone numbers hardcoded into an array (providerNumbers) so I think it's looping through three times because of this.
What I'm trying to do is eventually populate the phone numbers array from user-provided phone numbers they store on Firebase for each user. So a different user will receive the same notification to let them know to visit the website.
Here's my Node.js code:
var providerNumbers = ['number1', 'number2', 'number3'];
var body = "..."
app.get('/testtwilio', function(req, res){
Promise.all(
providerNumbers.map(number => {
return twilio.messages.create({
to: number,
from: '+15704058347',
body: body
});
})
).then(messages => {
console.log('Messages Sent!');
}).catch(err => console.error(err));
});
Anyone know how I can prevent the message sending three times to all the users?
There is nothing wrong with the code you posted.
.map calls the callback function once for each element in your providerNumbers array.
The callback function sends only one message when executed.
You can verify this if you log the index with something like this:
app.get('/testtwilio', function(req, res){
Promise.all(
providerNumbers.map((number, index) => {
console.log(index);
return twilio.messages.create({
to: number,
from: '+15704058347',
body: body
});
})
).then(messages => {
console.log('Messages Sent!');
}).catch(err => console.error(err));
});
In conclusion, something must be hitting your endpoint '/testtwilio' three times.
I hope this helps.
Your example seems to work, try checking the logs to see if multiple requests are being made to the same endpoint. A more concise method of mapping an array of primitives to promises would be using Promise.map. Reducing the usage of complex unit structures reduces the number of procedures your code has to perform, makes it easier to debug and to rule out false positives.
const providerNumbers = ['number1', 'number2', 'number3']
const from = '+15704058347'
const body = "..."
app.get('/testtwilio', (req, res, next) => {
Promise.map(
providerNumbers,
to => twilio.messages.create({to, from, body})
).then(res => {
console.log('Messages sent')
res.json({success: true})
}).catch(next)
})
I solved the problem by changing app.get to app.post.

Categories