I have a sign up button which inserts data in mongo db collection only if the data is unique otherwise the user should stay on the same page. For implementing the same , I am doing upsert:true. This is my code for node js
var mongoClient=require('mongodb').MongoClient;
var url='mongodb://localhost:27017/test';
app.post('/newuser', function(req, res) {
username=req.body.username;
password=req.body.password;
mongoClient.connect(url,function(err,db){
//console.log("connected")
db.collection('users',function(err,collection){
collection.update({username:username, password:password},{upsert:true},function(err,result){
res.send(result.insertedCount);
});
})
})
});
And the code for the frontend is
$.ajax({
type:'POST',
url:'/newuser',
data:{
username:username,
password:password
},
success:function(result){
console.log(result);
if(result===1){
that.setState({
isLoggedin:true
})
}else{
alert("not inserted");
}
}
})
return false;
}
Every time I run the server, the success function doesn't get executed.What is wrong with the code? Thanks.