javascript - Check if a MongoDB value exists before inserting - Stack Overflow

I'm making a MEAN app w Twilio that allows people to sign an online petition by texting their nam

I'm making a MEAN app w/ Twilio that allows people to sign an online petition by texting their name to the app. People should be able to sign the petition only once. I plan on ensuring this by allowing only one signature per phone number.

Every time the petition is signed a Mongo object like this will be created:

{ 
  "_id" : ObjectId("5c5a47ae8f04f9148f43b033"),          
  "name" : "John Doe", 
  "number" : "+18373987466", 
  "date" : "2/5/19" 
}

I've already turned the number field into a unique index, so there can be only signature per phone number.

The problem I'm ing across is what to do when someone tries to sign the petition multiple times. I want to intercept their attempt and send them a message saying that they've already signed the petition.

As I see it I have two options:

OPTION #1 Query the database to see if the same number has already been entered.

This seems like something that should be easy but I just can't figure out how to do it.

var number = '+18373987466';
if(collection.find({"number":number}) {
  twiml.message("You have already signed the petition");
}

My thinking here is thatcollection.find({"number":number}) would return true if it found an entry with the same number. However, it returns the entire Mongo cursor.

OPTION #2 Since number is a unique index, Mongo will throw an error if another object is created with the same value. I could intercept the error that is thrown and send a message to the user afterwards.

My first question with this approach is: is it good software design to knowingly allow your app to throw an error?

I tried this approach with a try/catch block but the error that was thrown crashed the app immediately.

try {
  collection.insertOne(
    {name : name, number : number, date: dateSigned}, 
    function(error, result) {
      console.log(`${name} has been added to the database`);
  });
} catch (error) {
  // handle error
}

Is this not the correct way to 'catch' an error and prevent it from crashing the app?

I'm making a MEAN app w/ Twilio that allows people to sign an online petition by texting their name to the app. People should be able to sign the petition only once. I plan on ensuring this by allowing only one signature per phone number.

Every time the petition is signed a Mongo object like this will be created:

{ 
  "_id" : ObjectId("5c5a47ae8f04f9148f43b033"),          
  "name" : "John Doe", 
  "number" : "+18373987466", 
  "date" : "2/5/19" 
}

I've already turned the number field into a unique index, so there can be only signature per phone number.

The problem I'm ing across is what to do when someone tries to sign the petition multiple times. I want to intercept their attempt and send them a message saying that they've already signed the petition.

As I see it I have two options:

OPTION #1 Query the database to see if the same number has already been entered.

This seems like something that should be easy but I just can't figure out how to do it.

var number = '+18373987466';
if(collection.find({"number":number}) {
  twiml.message("You have already signed the petition");
}

My thinking here is thatcollection.find({"number":number}) would return true if it found an entry with the same number. However, it returns the entire Mongo cursor.

OPTION #2 Since number is a unique index, Mongo will throw an error if another object is created with the same value. I could intercept the error that is thrown and send a message to the user afterwards.

My first question with this approach is: is it good software design to knowingly allow your app to throw an error?

I tried this approach with a try/catch block but the error that was thrown crashed the app immediately.

try {
  collection.insertOne(
    {name : name, number : number, date: dateSigned}, 
    function(error, result) {
      console.log(`${name} has been added to the database`);
  });
} catch (error) {
  // handle error
}

Is this not the correct way to 'catch' an error and prevent it from crashing the app?

Share Improve this question asked Feb 9, 2019 at 21:09 Matt SanchezMatt Sanchez 6031 gold badge7 silver badges10 bronze badges 2
  • Have you tried using mongoose? It's usually my choice when I work with node.js and mongodb. And as it provides Schemas a unique field is pretty simple then. mongoosejs./docs/schematypes.html This should clarify things a little. The documentation is actually pretty good – relief.melone Commented Feb 9, 2019 at 21:12
  • My database needs for this app are so simple that Mongoose seemed like overkill. – Matt Sanchez Commented Feb 9, 2019 at 21:16
Add a ment  | 

2 Answers 2

Reset to default 3

Option #1

The described behavior is correct, MongoDB will return a cursor if you use collection.find(). You could use collection.count(). But since you are only interested if there is already at least one value, you can just call cursor.limit(1).

if (collection.find({"number":number}).limit(1).length === 1) 
   twiml.message("You have already signed the petition");

Option #2

Is it good software design to knowingly allow your app to throw an error?

No. I consider this bad design. If the entry already exists I would not consider it an error but a case that needs special treatment. If you use catch() you would still need to check what kind of error it is to send the correct message to the user.
Imagine your database is not reachable therefore you can't insert a new value this would be a (real) error and you should catch() it.

var number = '+18373987466';
result = collection.find_one({"number":number}) 
if result is not None:
    twiml.message("You have already signed the petition");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745332169a4622932.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信