我正在使用以下节点模块,因为我想阻止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)
我正在尝试清理以下信息并创建一个JSON对象:
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
}
我将模块放在我的代码顶部:sanitize = require('validator').sanitize;
来自模块的xss.js的第178-183行:
function remove_invisible_characters(str) {
for (var i in non_displayables) {
str = str.replace(non_displayables[i], '');
}
return str;
}
答案 0 :(得分:3)
从错误中,看起来你正在将0
(一个数字)传递给正在执行str = str.replace(...);
行的任何函数。数字没有replace
功能。如果该函数需要并且要求其输入为字符串,则可以确保使用str = String(str).replace(...);
String
构造函数,在没有new
的情况下调用时,进行类型转换; details