2

Im creating an api for sign-up with mongodb as backend. this code throws an error like unexpected token (if statement)

please check with the logic & also i need to bcrypt my password before saving into db, i never done that before.

please help me to solve this issue

var express = require('express');
var router = express.Router();
var app = express();
var bodyParser = require("body-parser");
var validator = require('validator');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

var MongoClient = require('mongodb').MongoClient;

var url="mongodb://localhost:27017";
var dbo;

MongoClient.connect(url, function (err, db) {

  if (err) throw err;

    dbo=db.db("hospital_api");

});

router.post('/login', function(req,res) {

  var name = req.body.name;
  var hospital_id = req.body.hospital_id;
  var gender = req.body.gender;
  var designation = req.body.designation;
  var username = req.body.username;
  var pwd = req.body.pwd;
  var confirm_pwd = req.body.confirm_pwd;


  var name = if((validator.isAlpha(name)) && (name.length>=3) )
  {
    return this.name;

    var hospital_id = if((hospital_id==12345) || (hospital_id==67890))
    {
      return this.hospital_id;

      var gender = if((gender == "male") || (gender== "female"))
      {
        return this.gender = gender;

        var designation = if(validator.isAlpha(designation))
        {
          return designation=this.designation;

          var username = if(validator.isEmail(username))
          {
            return this.username = username;

            var pwd = if(pwd.length>=3)
            {
              this.pwd = pwd;

                dbo.collection("doctor").insertOne(req.body, function(err,res){
                  if(err) throw err;
                  res.send(req.body);
                });
            } else {

              res.send("password length is invalid")
            }
          } else {

            res.send("username should be in email format")
          }
        } else {
          res.send("designation length is not valid")
        }
      } else {
        res.send("enter only male or female")
      }
    } else {
      res.send("hospital id is not valid")
    }
  } else {
    res.send("please enter a valid name")
  }

});

module.exports = router;
Zoe
  • 27,060
  • 21
  • 118
  • 148
LogaKrishnan
  • 491
  • 1
  • 9
  • 24

1 Answers1

4

You cannot assign an if statement to a variable

Instead you can do something like this:

let val = null;
if((validator.isAlpha(name)) && (name.length>=3) ) {
    // Use proper assignment statement to assign a value to variable rather than val = name<br>.
    val = name;
}
Dipak
  • 2,248
  • 4
  • 22
  • 55
Arpan Shah
  • 159
  • 9