使用mongodb在nodejs中的用户注册api

时间:2018-03-18 12:28:15

标签: node.js mongodb api node-modules nodeapi

我正在创建api注册,mongodb作为后端。此代码抛出一个错误,如意外令牌(if statement

请检查逻辑&在保存到db之前我还需要bcrypt我的密码,我之前从未这样做过。

请帮我解决这个问题

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;

1 个答案:

答案 0 :(得分:4)

您不能将if语句分配给变量

相反,你可以这样做:

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;
}