错误引发新错误('无效范围'+ scopeName +'被调用。')

时间:2018-11-27 16:42:14

标签: node.js sequelize.js

我的错误是:-

server\node_modules\sequelize\lib\model.js:1224
      throw new Error('Invalid scope ' + scopeName + ' called.');
      ^

Error: Invalid scope nested called.
    at D:\Coworkee-master_org(V8)\server\node_modules\sequelize\lib\model.js:1224:13
    at Array.forEach (<anonymous>)
    at Model.scope (D:\Coworkee-master_org(V8)\server\node_modules\sequelize\lib\model.js:1182:11)
    at Model.associate (D:\Coworkee-master_org(V8)\server\models\employee.js:191:47)
    at D:\Coworkee-master_org(V8)\server\models\index.js:26:19
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (D:\Coworkee-master_org(V8)\server\models\index.js:24:17)
    at Module._compile (internal/modules/cjs/loader.js:654:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
    at Function.Module._load (internal/modules/cjs/loader.js:498:3)
    at Module.require (internal/modules/cjs/loader.js:598:17)
    at require (internal/modules/cjs/helpers.js:11:18)
    at Object.<anonymous> (D:\Coworkee-master_org(V8)\server\utils\data.js:3:14)
    at Module._compile (internal/modules/cjs/loader.js:654:30)
[nodemon] app crashed - waiting for file changes before starting...

我的模特是:- 索引:-

/ **  *此文件从models目录中收集所有模型,并在需要时将它们关联。  * /

    "use strict";

    var fs = require("fs");
    var path = require("path");
    var Sequelize = require("sequelize");
    var env = process.env.NODE_ENV || "development";
    var config = require(path.join(__dirname, '..', 'utils', 'config')).database;
    var sequelize = new Sequelize(config.database, config.username, config.password, config);
    var db = {};

    fs.readdirSync(__dirname)
      .filter(function(file) {
        return (file.indexOf(".") !== 0) && (file !== "index.js");
      })
      .forEach(function(file) {
        var model = sequelize.import(path.join(__dirname, file));
        db[model.name] = model;
      });

    Object.keys(db).forEach(function(modelName) {
      if ("associate" in db[modelName]) {
        db[modelName].associate(db);
      }
    });

    db.sequelize = sequelize;
    db.Sequelize = Sequelize;

    module.exports = db;



    Employee : 
    "use strict";

    var errors = require('../utils/errors');
    var helpers = require('../utils/helpers.js');

    module.exports = function(sequelize, DataTypes) {
        var Model = sequelize.define("Employee", {
            id: {
                type: DataTypes.UUID,
                defaultValue: DataTypes.UUIDV4,
                allowNull: false,
                primaryKey: true,
                validate: {
                    isUUID: 4
                }
            },
            employeeid:{
                type: DataTypes.STRING,
                allowNull: true,
                searchable : true,
                unique : {
                    msg : 'Id already Taken'
                }
            },
            email: {
                type: DataTypes.STRING,
                allowNull: false,
                searchable: true,
                unique: {
                    msg: 'This email is already taken.'
                },
                validate: {
                    isEmail: true
                }
            },
            username: {
                type: DataTypes.STRING,
                allowNull: false,
                unique: {
                    msg: 'This username is already taken.'
                },
                validate: {
                    len: 6
                }
            },
            contactnumber: {
                type: DataTypes.STRING,
                allowNull: true,
                searchable: true
            },
            password: {
                type: DataTypes.STRING,
                allowNull: false,
                validate: {
                    notEmpty: true
                }
            },
            firstname: {
                type: DataTypes.STRING,
                allowNull: false,
                searchable: true,
                validate: {
                    notEmpty: true
                }
            },
            lastname: {
                type: DataTypes.STRING,
                allowNull: false,
                searchable: true,
                validate: {
                    notEmpty: true
                }
            },
            title: {
                type: DataTypes.STRING,
                allowNull: true,
                searchable: true
            },
            phone: {
                type: DataTypes.STRING,
                allowNull: true,
                searchable: true
            },
            extension: {
                type: DataTypes.STRING,
                allowNull: true,
                searchable: true
            },
            skype: {
                type: DataTypes.STRING,
                allowNull: true,
                searchable: true
            },
            linkedin: {
                type: DataTypes.STRING,
                allowNull: true,
                searchable: true
            },
            picture: {
                type: DataTypes.STRING,
                allowNull: true,
                get: function() {
                    return helpers.apiUrl + this.getDataValue('picture');
                }
            },
            birthday: {
                type: DataTypes.DATEONLY,
                allowNull: false,
                validate: {
                    isDate: true
                }
            },
            started: {
                type: DataTypes.DATEONLY,
                allowNull: true,
                validate: {
                    isDate: true
                }
            },
            ended: {
                type: DataTypes.DATEONLY,
                allowNull: true,
                validate: {
                    isDate: true
                }
            },
            dateofjoining: {
                type: DataTypes.DATEONLY,
                allowNull: true,
                validate: {
                    isDate: true
                }
            },
            employeetype: {
                type: DataTypes.STRING,
                allowNull: true,
                searchable: true
            },
            addressline1: {
                type: DataTypes.STRING,
                allowNull: true,
                searchable: true
            },
            addressline2: {
                type: DataTypes.STRING,
                allowNull: true,
                searchable: true
            },
            city: {
                type: DataTypes.STRING,
                allowNull: true,
                searchable: true
            },
            state: {
                type: DataTypes.STRING,
                allowNull: true,
                searchable: true
            },
            pincode: {
                type: DataTypes.BIGINT,
                allowNull: true,
                searchable: true
            },
            createdby: {
                type: DataTypes.BIGINT,
                allowNull: true,
                searchable: true
            },
            updatedby: {
                type: DataTypes.BIGINT,
                allowNull: true,
                searchable: true
            }
        }, {
            defaultScope: {
                attributes: {
                    exclude: ['password']
                }
            },

            classMethods: {
                associate: function(models) {
                    Model.belongsTo(models.Department, { as : 'department'});
                    Model.belongsTo(models.Office, { as: 'office' });
                    Model.hasMany(models.Action, { as: 'actions' });
                    Model.addScope('nested', {
                        attributes: {
                            exclude: ['password']
                        },
                        include: [
                           { model: models.Office.scope('nested'), as: 'office' },
                           { model: models.Department.scope('nested'),  as : 'department'}
                        ]
                    }) ; 
                },

                lookup: function(identifier) {
                    return this.findOne({
                        where: {
                            $or: [
                                { id: identifier },
                                { username: identifier },
                                { email: identifier }
                            ]
                        }
                    }).then(function(row) {
                        if (!row) {
                            throw errors.generate('Unknown Employee with id/username/email: ' + identifier);
                        }
                        return row;
                    });
                },

                sayHello: function(){
                    console.log('Hello! I am ' , Model.name);
                } 
            }
        });

        return Model;
    };

    Department:-

    "use strict";

    module.exports = function(sequelize, DataTypes) {
        var Model = sequelize.define("Department", {
            id: {
                type: DataTypes.UUID,
                defaultValue: DataTypes.UUIDV4,
                allowNull: false,
                primaryKey: true,
                validate: {
                    isUUID: 4
                }
            },  
            name: {
                type: DataTypes.STRING,
                allowNull: false,


  searchable: true,
            unique: {
                msg: 'An organization with this name already exists.'
            },
            validate: {
                notEmpty: true
            }
        }
    },{
        //tableName: 'organization',
        classMethods: {
            associate: function(models) {
                Model.hasMany(models.Employee, { as: 'employees' });
                Model.belongsTo(models.Employee, { as: 'manager', constraints: false });

                // http://stackoverflow.com/a/37817966
                Model.addScope('nested', {
                    attributes: {
                        include: [[sequelize.literal('(SELECT COUNT(*) FROM Employees WHERE Employees.department_id = Department.id)'), 'headcount']]
                    },
                    include: [{
                        model: models.Employee,
                        as: 'manager',
                        include: [{
                            model: models.Office,
                            as: 'office'
                        }]
                    }]
                });
            }
        }
    });

    return Model;
};

Office:-

"use strict";

module.exports = function(sequelize, DataTypes) {
    var Model = sequelize.define("Office", {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            allowNull: false,
            primaryKey: true,
            validate: {
                isUUID: 4
            }
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false,
            searchable: true,
            unique: {
                msg: 'An office with this name already exists.'
            },
            validate: {
                notEmpty: true
            }
        },
        address: {
            type: DataTypes.STRING,
            allowNull: false,
            searchable: true,
            validate: {
                notEmpty: true
            }
        },
        postcode: {
            type: DataTypes.STRING,
            allowNull: true,
            searchable: true
        },
        region: {
            type: DataTypes.STRING,
            allowNull: true,
            searchable: true
        },
        city: {
            type: DataTypes.STRING,
            allowNull: false,
            searchable: true,
            validate: {
                notEmpty: true
            }
        },
        country: {
            type: DataTypes.STRING,
            allowNull: false,
            searchable: true,
            validate: {
                notEmpty: true
            }
        },
        location: {
            type: DataTypes.TEXT,
            allowNull: false,
            get: function () {
                return JSON.parse(this.getDataValue('location'));
            },
            set: function (value) {
                return this.setDataValue('location', JSON.stringify(value));
            }
        }
    }, {
        classMethods: {
            associate: function(models) {
                 Model.hasMany(models.Employee, { as: 'employees' });

                 // http://stackoverflow.com/a/37817966
                 Model.addScope('nested', {
                    attributes: {
                        include: [[sequelize.literal('(SELECT COUNT(*) FROM Employees WHERE Employees.office_id = Office.id)'), 'headcount']]
                    }
                 });
            }
        }
    });

    return Model;
};

Action:_
"use strict";

module.exports = function(sequelize, DataTypes) {
    var Model = sequelize.define("Action", {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            allowNull: false,
            primaryKey: true,
            validate: {
                isUUID: 4
            }
        },
        type: {
            type: DataTypes.STRING,
            allowNull: false,
            searchable: true,
            validate: {
                notEmpty: true
            }
        },
        subject: {
            type: DataTypes.STRING,
            searchable: true,
            validate: {
                notEmpty: true
            }
        }
    }, {
        classMethods: {
            associate: function(models) {
                Model.belongsTo(models.Employee, { as: 'recipient', constraints: false });
                Model.belongsTo(models.Employee);
                Model.addScope('nested', {
                    include: [{
                        model: models.Employee,
                        as: 'recipient',
                        include: [{
                            model: models.Office,
                            as: 'office'
                        }, {
                            model: models.Department,
                            as: 'department'
                        }]
                    }]
                });
            },

            subject: function(action, recipient) {
                switch (action) {
                case 'phone':
                    var extension = recipient.get('extension');
                    return recipient.get('phone') + (extension? ':' + extension : '');
                case 'profile':
                    return recipient.get('username');
                case 'email':
                case 'linkedin':
                case 'skype':
                    return recipient.get(action);
                default:
                    return null;
                }
            }
        }
    });

    return Model;
};

有人可以告诉我如何解决吗?

0 个答案:

没有答案