sequelize: Unhandled rejection TypeError: val.replace is not a function

I’m thinking that this is the same problem to #5082 however I will go ahead and post all of my code related to the model and the problem.

Model:

/* jshint indent: 2 */

module.exports = function (sequelize, DataTypes) {
    return sequelize.define('users', {
        id: {
            type: DataTypes.INTEGER(10),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        email: {
            type: DataTypes.STRING,
            allowNull: false
        },
        password: {
            type: DataTypes.STRING,
            allowNull: false
        },
        permissions: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        last_login: {
            type: DataTypes.DATE,
            allowNull: true
        },
        username: {
            type: DataTypes.STRING,
            allowNull: false
        },
        created_at: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: '0000-00-00 00:00:00'
        },
        updated_at: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: '0000-00-00 00:00:00'
        },
        deleted_at: {
            type: DataTypes.DATE,
            allowNull: true
        }
    }, {
        tableName: 'users',
        freezeTableName: true
    });
};

Setup:

Sequelize = require('sequelize');
orm = new Sequelize(config.get('mysql.database'), config.get('mysql.username'), config.get('mysql.password'), {
    host: config.get('mysql.host'),
    port: config.get('mysql.port'),
    dialect: 'mysql',
    define: {
        timestamps: false
    }
});

Usage:

var mentions = function (message) {
    var userModel = orm.import(__dirname + '/models/users.js');
    // Match twitter like mentions
    var usernames = message.match(/\B@[a-z0-9_-]+/gi);
    var realUsers = {};
    // Verify that the user exist in the database
    var verify = function(element, index, array) {
        console.log(element);
        userModel.find({
            where: {
                username: element.replace('@', '')
            }
        }).then(function(data) {
            if(data != null) {
                realUsers[data.username]
            }
        })
    };
    // If there are mentioned user check them!
    if(usernames !== null) {
        usernames.forEach(verify);
        return realUsers;
    } else {
        // Return empty if there are no mentioned users.
        return {};
    }
};

And actually calling the mention function:

var json = JSON.parse(data);
var mentionsObj = mentions(json.message);

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 16 (7 by maintainers)

Most upvoted comments

… probably you assign something diffrent from what is expected. (object instead of string, integer)

layers.map returns an array of promises, not an array of instances - You need to wait for the create calls to return. Have a look at Promise.all