Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.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 节点续集如何编写静态函数_Javascript_Mysql_Node.js_Sequelize.js - Fatal编程技术网

Javascript 节点续集如何编写静态函数

Javascript 节点续集如何编写静态函数,javascript,mysql,node.js,sequelize.js,Javascript,Mysql,Node.js,Sequelize.js,我有在moongose中编写静态函数的经验 var mongoose =require('mongoose'); var Schema = mongoose.Schema; var adminSchema = new Schema({ fullname : String, number : Number, email: String, auth : { username: String, password : String,

我有在moongose中编写静态函数的经验

    var mongoose =require('mongoose');
var Schema = mongoose.Schema;
var adminSchema = new Schema({
    fullname : String,
    number : Number,
    email: String,
    auth : {
        username: String,
        password : String,
        salt: String
    }

});


adminSchema.statics.usernameInUse = function (username, callback) {
    this.findOne({ 'auth.username' : username }, function (err, doc) {
        if (err) callback(err);
        else if (doc) callback(null, true);
        else callback(null, false);
    });
};
这里usernameInUse是我写的函数,但它使用sequelize for mysql数据库

我的模型

 /*
  This module is attendant_user table model.
  It will store attendants accounts details.
*/

"use strict";

module.exports = function(sequelize, DataTypes) {
  var AttendantUser = sequelize.define('AttendantUser', {

    username : {
      type : DataTypes.STRING,
      allowNull : false,
      validate : {
        isAlpha : true
      }
    },{
    freezeTableName : true,
    paranoid : true
  });

  return AttendantUser;
};
如何在这里添加静力学函数..???

嗯,您可以轻松使用

AttendantUser.usernameInUse = function (username, callback) {
   ...
};
return AttendantUser;
或者在某些情况下,您可以在模型上使用getter和setter。见:

定义为属性的一部分:

B定义为模型的一部分:

希望能有帮助

var User = sequelize.define('user', { firstname: Sequelize.STRING });

// Adding a class level method
User.classLevelMethod = function() {
  return 'foo';
};

// Adding an instance level method
User.prototype.instanceLevelMethod = function() {
  return 'bar';
};
var Employee = sequelize.define('employee', {
  name:  {
    type     : Sequelize.STRING,
    allowNull: false,
    get      : function()  {
      var title = this.getDataValue('title');
      // 'this' allows you to access attributes of the instance
      return this.getDataValue('name') + ' (' + title + ')';
    },
  },
  title: {
    type     : Sequelize.STRING,
    allowNull: false,
    set      : function(val) {
      this.setDataValue('title', val.toUpperCase());
    }
  }
});

Employee
  .create({ name: 'John Doe', title: 'senior engineer' })
  .then(function(employee) {
    console.log(employee.get('name')); // John Doe (SENIOR ENGINEER)
    console.log(employee.get('title')); // SENIOR ENGINEER
  })
var Foo = sequelize.define('foo', {
  firstname: Sequelize.STRING,
  lastname: Sequelize.STRING
}, {
  getterMethods   : {
    fullName       : function()  { return this.firstname + ' ' + this.lastname }
  },

  setterMethods   : {
    fullName       : function(value) {
        var names = value.split(' ');

        this.setDataValue('firstname', names.slice(0, -1).join(' '));
        this.setDataValue('lastname', names.slice(-1).join(' '));
    },
  }
});