Jack
Jack

Reputation: 15872

Object has no method replace in node-validator module

I'm using the following node module because I would like to prevent XSS: https://github.com/chriso/node-validator

        str = str.replace(non_displayables[i], '');
                  ^
TypeError: Object 0 has no method 'replace'
    at remove_invisible_characters (C:\Users\Jack\Desktop\Dropbox\Nimble\node_mo
dules\validator\lib\xss.js:180:19)

I'm trying to sanitize the following information and create a JSON object:

var story = {
  _id:          sanitize(data._id).xss(),         //string
  title:        sanitize(data.title).xss(),       //string
  deadline:     sanitize(data.deadline).xss(),    //date string
  description:  sanitize(data.description).xss(), //string
  sp:           sanitize(data.sp).xss(),          //number
  value:        sanitize(data.value).xss(),       //number
  roi:          sanitize(data.roi).xss(),         //decimal number
  type:         sanitize(data.type).xss(),        //string
  lane:         sanitize(data.lane).xss()         //Typically 0
}

I'm including the module at the top of my code: sanitize = require('validator').sanitize;

Lines 178 - 183 of xss.js from the module:

function remove_invisible_characters(str) {
    for (var i in non_displayables) {
        str = str.replace(non_displayables[i], '');
    }
    return str;
}

Upvotes: 0

Views: 3674

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074979

From the error, it looks like you're passing 0 (a number) into whatever function it is that's doing the str = str.replace(...); line. Numbers don't have the replace function. If that function expects and requires that its input be a string, you can ensure that by using str = String(str).replace(...); The String constructor, when called without new, does a type conversion; details.

Upvotes: 3

Related Questions