Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angularjs检查电子邮件是否已经存在_Javascript_Angularjs - Fatal编程技术网

Javascript angularjs检查电子邮件是否已经存在

Javascript angularjs检查电子邮件是否已经存在,javascript,angularjs,Javascript,Angularjs,我正在尝试实现提供的答案,如果电子邮件已经在数据库中,则会抛出错误。这是我当前的指令,它抛出一个未定义用户的错误。我已经有了一个定义用户的User.js文件,它被导入到api.js文件中。我不知道如何访问链接到数据库的后端User.js文件 'use strict'; myApp.directive('emailExists', function($timeout, User) { return { restrict: 'AE', require: 'ngModel',

我正在尝试实现提供的答案,如果电子邮件已经在数据库中,则会抛出错误。这是我当前的指令,它抛出一个未定义用户的错误。我已经有了一个定义用户的
User.js
文件,它被导入到
api.js
文件中。我不知道如何访问链接到数据库的后端
User.js
文件

'use strict';

myApp.directive('emailExists', function($timeout, User) {
  return {
    restrict: 'AE',
    require: 'ngModel',
    link: function(scope, elm, attr, model) {
      model.$asyncValidators.usernameExists = function(email) {
        var searchUser = {
          email: email
        };
        User.findOne(searchUser, function (user) {
          if(user)     
          then(function(res){+
          $timeout(function(){
        model.$setValidity('usernameExists', !!res.data);
      }, 1000);
    });
            });

      };
    }
  }
});
更新
根据提供的答案更新了代码,我忘记了包含用户,但是,我仍然得到一个错误:


User.js
是包含在我的
localStrategy.js
中的一个模型,这是一个导入到我的
api.js
的服务,我在grunt服务旁边运行该文件作为
nodemon api.js
,以启动我的应用程序。

这是我的User.js文件:

var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');

var UserSchema = new mongoose.Schema({
  email: String,
  password: String,
  displayName: String,
  active: Boolean
});

UserSchema.methods.toJSON = function () {
  var user = this.toObject();
  delete user.password;
  return user;
};

UserSchema.methods.comparePasswords = function (password, callback) {
  bcrypt.compare(password, this.password, callback);
}

UserSchema.pre('save', function (next) {
  var user = this;

  if (!user.isModified('password')) return next();

  bcrypt.genSalt(10, function (err, salt) {
    if (err) return next(err);

    bcrypt.hash(user.password, salt, null, function (err, hash) {
      if (err) return next(err);

      user.password = hash;
      next();
    })
  })
})

module.exports = mongoose.model('User', UserSchema);

您必须将其注入指令,就像注入
$timeout
一样:

myApp.directive('emailExists', function($timeout, User) {
    ...
});

谢谢我更新了我的问题,这是真的,我忘了包括它,我仍然得到一个错误。只是为了确保,您的user.js被放在指令文件上方的html中,对吗?这意味着
user
没有以Angular理解的方式定义。
User
是服务吗?我们必须了解您是如何定义它并将其注册为模块以提供帮助的。