question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Example of the 'val.replace is not a function' error message

See original GitHub issue

All, 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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
jmlsfcommented, Sep 14, 2016

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 addPicture example 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.

0reactions
felixfbeckercommented, May 8, 2017

@YanetP88 don’t think the cause is the same, if you think there is a bug please open an issue with an SSCCE

Read more comments on GitHub >

github_iconTop Results From Across the Web

val.replace is not a function - sequelize.js - Stack Overflow
I keep getting this error every-time I execute the code below, ... Unhandled rejection TypeError: val.replace is not a function at Object.
Read more >
TypeError: replace is not a function in JavaScript | bobbyhadz
The "replace is not a function" error occurs when we call the replace() method on a value that is not of type string...
Read more >
TypeError: replace is not a function in JavaScript - Java2Blog
TypeError: .replace is not a function occurs when we call replace() function on object which is not an string. replace() function can be...
Read more >
Solved: "replaceAll is not a function" JavaScript TypeError
Learn how to fix the "replaceAll() is not a function" JavaScript error.
Read more >
TypeError: "x" is not a function - JavaScript - MDN Web Docs
Examples · A typo in the function name · Function called on the wrong object · Function shares a name with a pre-existing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found