0

I am looking to do some registration form validation that when submitted it checks the database for existing data matches.

// Check if username is available or not
var existsInDatabase = function(field, value){
  console.log('Working with: ', field + ' ' + value); // Console logs the appropriate fields & values
  User.find({ field : value }, function(err, docs){
    if(docs){
      console.log(docs); // console.log shows []
    }
  });
};

If change my User.find() query to have the variables I am passing into the function it pulls finds the resource in Mongo.

Thanks in advance for any help/guidance.

1 Answer 1

2

You can try:

  var query = {};
  query[field] = value;
  User.find(query, function(err, docs){
    if(docs){
      console.log(docs); // console.log shows []
    }
  });

And if you are using ES6, you can:

  User.find({ [field] : value }, function(err, docs){
    if(docs){
      console.log(docs); // console.log shows []
    }
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.