Giter VIP home page Giter VIP logo

Comments (6)

wojtek-krysiak avatar wojtek-krysiak commented on May 22, 2024 1

most probably it is due to the fact that sequelize changed interface in the latest version. i will update it during the weekend and let you know

from adminjs.

wojtek-krysiak avatar wojtek-krysiak commented on May 22, 2024

Hi - thanks for issue :).

Can you write what import sqModels (from '../sq-models') are in your case and what is the schema of your model (sqModels.User from the example)

from adminjs.

stackedq avatar stackedq commented on May 22, 2024

sqModels index.js:

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/seq.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs.readdirSync(__dirname).filter(file => {
  return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
}).forEach(file => {
  const model = sequelize.import (path.join(__dirname, file));
  db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

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

module.exports = db;

And User model:


module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    name: {
      type: DataTypes.STRING,
      validate: {
        len: [0, 30]
      }
    },
    username: {
      type: DataTypes.STRING,
      allowNull: {
        args: false,
        msg: 'Please enter your username'
      },
      unique: {
        args: true,
        msg: 'Sorry, this username is taken'
      }
    },
    email: {
      type: DataTypes.STRING,
      unique: {
        args: true,
        msg: 'Sorry, this username is taken'
      },
      validate: {
        isEmail: {
          args: true,
          msg: 'Please enter a valid email address'
        }
      }
    },
    password: {
      type: DataTypes.STRING,
      allowNull: {
        args: false,
        msg: 'Please enter a password'
      },
      validate: {
        isNotShort: (value) => {
          if (value.length < 8) {
            throw new Error('Password should be at least 8 characters');
          }
        }
      }
    },
    bio: {
      type: DataTypes.STRING,
      validate: {
        len: [0, 150]
      }
    },
    cuid: {
      type: DataTypes.STRING,
      allowNull: true
    },
    phoneNumber: {
      type: DataTypes.STRING
    },
    isPrivate: {
      type: DataTypes.BOOLEAN
    },
    isSuperUser: {
      type: DataTypes.BOOLEAN,
      allowNull: true
    },
    needToVerifyEmail: {
      type: DataTypes.BOOLEAN
    },
    needToVerifyPhoneNumber: {
      type: DataTypes.BOOLEAN
    },
    emailVerificationNumber: {
      type: DataTypes.INTEGER
    },
    cellphoneVerificationNumber: {
      type: DataTypes.INTEGER
    },
    followerCount: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },
    followingCount: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },
    qoins: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },
    image: {
      type: DataTypes.STRING
    }
  }, {});
  User.associate = models => {
    User.hasMany(models.UserImage, {
      as: 'imagesPath',
      constraints: false,
      allowNull: true
    });
    User.hasMany(models.Relation, {
      as: 'selfRelations',
      foreignKey: 'fromUser',
      constraints: false,
      defaultValue: null
    });
    User.hasMany(models.Relation, {
      as: 'othersRelations',
      foreignKey: 'toUser',
      constraints: false,
      defaultValue: null
    });
  };
  User.findByUsername = async (username, passId) => {
    let filter = {
      where: {
        username: username
      },
      attributes: [
        'followerCount',
        'followingCount',
        'image',
        'qoins',
        'isPrivate',
        'name',
        'bio',
        'id',
        'username'
      ],
      include: ['imagesPath']
    }
    if (passId)
      filter.attributes.push('id')
    let user = await User.findOne(filter);
    return user
  }
  User.getFullUser = async username => {
    let user = await User.findOne({
      where: {
        username: username
      },
      attributes: {
        exclude: ['emailVerificationNumber', 'cellphoneVerificationNumber', 'createdAt', 'updatedAt', 'password']
      },
      include: ['imagesPath']
    });
    return user
  }
  return User;
};

from adminjs.

MattRay0295 avatar MattRay0295 commented on May 22, 2024

@wojtek-krysiak Awesome, I was getting the same thing with MySQLv5.7.23 and I was only grabbing the name from the users table, I couldn't figure out why I was getting that TypeError: Cannot convert undefined or null to object. 😅

from adminjs.

wojtek-krysiak avatar wojtek-krysiak commented on May 22, 2024

Gents - fixed that in admin-bro-sequelizejs v0.2.2 - please update your package.json files and let me know if it still occurs.

from adminjs.

stackedq avatar stackedq commented on May 22, 2024

It works, thanks.

from adminjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.