Example of the 'val.replace is not a function' error message
See original GitHub issueAll, I see that there are several reports of the ‘val.replace is not a function’ error, and it doesn’t seem like people have been able to provide complete, minimal examples, so I’m doing that here. See #6530 #5292 #5082 #5185. No doubt I am doing something obviously wrong here, but I thought I’d submit this in the hopes that (a) you can help me figure out what I am missing, and (b) perhaps the error message could be improved with a stack trace or other hint back into user code.
I have attached a very simple example of this issue. You just need to drop in a config.json and run npm install. sequelizebug.tar.gz
models/user.js:
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
name: {
type: DataTypes.TEXT,
allowNull: false
}
}, {
classMethods: {
associate: (models) => {
User.hasMany(models.Doc);
}
}
});
return User;
};
models/doc.js:
module.exports = (sequelize, DataTypes) => {
const Doc = sequelize.define('Doc', {
originalName: {
type: DataTypes.TEXT,
allowNull: false
}
}, {
classMethods: {
}
});
return Doc;
};
index.js:
const models = require('./models');
models.sequelize.sync({ force: true })
.then(() => {
return models.User.create({ name: 'Name' })
.then(user => {
return user.addDoc({ name: 'DocName' });
})
.then(doc => {
console.log('success');
})
.catch(e => {
console.log(e);
});
});
This is the difficult-to-interpret stacktrace:
~/workspace/sequelizebug$ node index.js
Executing (default): INSERT INTO `Users` (`id`,`name`,`createdAt`,`updatedAt`) VALUES (DEFAULT,'Name','2016-09-14 20:40:52','2016-09-14
20:40:52');
TypeError: val.replace is not a function
at Object.SqlString.escape (/Users/justinlee/workspace/sequelizebug/node_modules/sequelize/lib/sql-string.js:63:15)
at Object.QueryGenerator.escape (/Users/justinlee/workspace/sequelizebug/node_modules/sequelize/lib/dialects/abstract/query-generat
or.js:979:22)
at /Users/justinlee/workspace/sequelizebug/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2280:23
at Array.map (native)
at Object.QueryGenerator.whereItemQuery (/Users/justinlee/workspace/sequelizebug/node_modules/sequelize/lib/dialects/abstract/query
-generator.js:2279:49)
at /Users/justinlee/workspace/sequelizebug/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1980:25
at /Users/justinlee/workspace/sequelizebug/node_modules/lodash/lodash.js:4389:15
at baseForOwn (/Users/justinlee/workspace/sequelizebug/node_modules/lodash/lodash.js:2652:24)
at Function.forOwn (/Users/justinlee/workspace/sequelizebug/node_modules/lodash/lodash.js:12254:24)
at Object.QueryGenerator.whereItemsQuery (/Users/justinlee/workspace/sequelizebug/node_modules/sequelize/lib/dialects/abstract/quer
y-generator.js:1979:9)
at Object.QueryGenerator.whereQuery (/Users/justinlee/workspace/sequelizebug/node_modules/sequelize/lib/dialects/abstract/query-gen
erator.js:1951:22)
at Object.QueryGenerator.updateQuery (/Users/justinlee/workspace/sequelizebug/node_modules/sequelize/lib/dialects/abstract/query-ge
nerator.js:482:19)
at QueryInterface.bulkUpdate (/Users/justinlee/workspace/sequelizebug/node_modules/sequelize/lib/query-interface.js:604:33)
at /Users/justinlee/workspace/sequelizebug/node_modules/sequelize/lib/model.js:2589:32
at tryCatcher (/Users/justinlee/workspace/sequelizebug/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/justinlee/workspace/sequelizebug/node_modules/bluebird/js/release/promise.js:510:31)
~/workspace/sequelizebug$
I’m using mysql 5.6.31 (via Google Cloud). Versions of libraries are:
"dependencies": {
"mysql": "^2.11.1",
"sequelize": "^3.24.3",
"sequelize-cli": "^2.4.0"
}
I hope this is helpful.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)

Top Related StackOverflow Question
I just realized that too: you are right. I thought that adding associations is how you insert new records, but I was mistaken. Friendly suggestion that the docs here (http://docs.sequelizejs.com/en/latest/api/associations/) would be really much better if the
addPictureexample showed or explained what object was being passed to the function. The problem with these association methods is that there really isn’t much detailed documentation on them like there is for methods that aren’t auto-generated.As for the unexpected error message: I added some logging statements in the code, and what happens is the
{ name: 'DocName' }object get passed all the way down to SqlString.escape. That function has a switch based on the type, but if it doesn’t match one of those, it assumes that it has been passed a string. Hence the message. I don’t understand the code base well enough to understand when and if there is a better place to perform some kind of error checking. Feel free to close.@YanetP88 don’t think the cause is the same, if you think there is a bug please open an issue with an SSCCE