Below is the code
User.js:
/*
* POST to adduser.
*/
router.post('/adduser', function(req, res) {
var db = req.db;
db.users.findOne({ inputUserEmail: inputUserEmail }, function(err, user) { if (user) {
console.log("user exists") } else { console.log("user doesn't exist") };
db.collection('userlist').insert(req.body, function(err, result){
res.send(
(err === null) ? { msg: '' } : { msg: err }
);
});
});
Below is the insert code:
global.js:
// Add User
function addUser(event) {
event.preventDefault();
// Super basic validation - increase errorCount variable if any fields are blank
var errorCount = 0;
$('#addUser input').each(function(index, val) {
if($(this).val() === '') { errorCount++; }
});
// Check and make sure errorCount's still at zero
if(errorCount === 0) {
// If it is, compile all user info into one object
var newUser = {
'username': $('#addUser fieldset input#inputUserName').val(),
'email': $('#addUser fieldset input#inputUserEmail').val(),
'fullname': $('#addUser fieldset input#inputUserFullname').val(),
'age': $('#addUser fieldset input#inputUserAge').val(),
'location': $('#addUser fieldset input#inputUserLocation').val(),
'gender': $('#addUser fieldset input#inputUserGender').val()
}
I am newbie to this technology, its a very basic question. But I am unable to code that.
Please tell me what I am doing wrong in user.js
or how to do so .
disclaimer: Never used node.js so maybe needs tweaking
You are probably selecting the wrong field in your find one you should probably look for {email: inputUserEmail}
instead of {inputUserEmail: inputUserEmail}
when I look a your insert document
You could make the database do the work for you the idea is to make mail
a unique index so that mongodb prevent duplicate insertion on a database level. The relevant part of the node.js driver documentations can be found here.
What I take from this is that
db.collection('userlist').createIndex("mail", true, function(err, result){}));
should make mail a unique index. This should only need to be executed once.
When you try to insert some mail
that already exists in the database your insert
should return an err
saying something like duplicate index
. So you insert could look like
router.post('/adduser', function(req, res) {
var db = req.db;
db.collection('userlist').createIndex("mail", true, function(err, result){}));
db.collection('userlist').insert(req.body, function(err, result){
if (err) {console.log("user exists") }
res.send(
(err === null) ? { msg: '' } : { msg: err }
);
});
});
This would build the index each time so it will take longer than it would if the index is created only once on some initialisation but then again you don't create users often compared to other db operations. It should probably look for a specific error ('duplicate index') or something that you can find out by trial and error.
Not 100% percent sure about the syntax so best viewed as pseudocode
You can also use the users email as the id field in your collection.
http://docs.mongodb.org/manual/tutorial/insert-documents/
var newUser = {
'_id': $('#addUser fieldset input#inputUserEmail').val(),
'username': $('#addUser fieldset input#inputUserName').val(),
'email': $('#addUser fieldset input#inputUserEmail').val(),
'fullname': $('#addUser fieldset input#inputUserFullname').val(),
'age': $('#addUser fieldset input#inputUserAge').val(),
'location': $('#addUser fieldset input#inputUserLocation').val(),
'gender': $('#addUser fieldset input#inputUserGender').val()
};
db.collection("userList").insert(newUser);
By using the above solution you don't have to worry about getting an additional index and maintaining that.
This issue belongs to mongodb rather than Bluemix,however you need to create a unique index on email field to avoid duplicate like below:
var newUser = {
'_id': $('#addUser fieldset input#inputUserEmail').val(),
'email': $('#addUser fieldset input#inputUserEmail').val(),
}
In this way only unique email -id will be inserted.for details on unique index,please refer below link: