Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法读取属性';拆分';javascript中字符串上未定义的_Javascript - Fatal编程技术网

无法读取属性';拆分';javascript中字符串上未定义的

无法读取属性';拆分';javascript中字符串上未定义的,javascript,Javascript,为什么会出现错误: TypeError: Cannot read property 'split' of undefined 使用以下代码 var testing = url.parse(req.url, true); testing = JSON.parse(JSON.stringify(testing.query)); console.log(testing); testing.q = testing.q.split(' ').join('+'); console.log(testing)

为什么会出现错误:

TypeError: Cannot read property 'split' of undefined
使用以下代码

var testing = url.parse(req.url, true);
testing = JSON.parse(JSON.stringify(testing.query));
console.log(testing);
testing.q = testing.q.split(' ').join('+');
console.log(testing);
第一个console.log打印:

{q: 'hello world'}
{q: 'hello+world'} //Which is correct BUT still giving me the error
第二个console.log打印: 第一个console.log打印:

{q: 'hello world'}
{q: 'hello+world'} //Which is correct BUT still giving me the error
例:

我希望它拆分并连接空间:

hello+world

这是因为在JSON对象上使用split()函数,而不是“hello world”字符串

var obj=JSON.parse(“{”name:“Hello World”}”);
obj.name=obj.name.split('').join('+');

控制台日志(obj)请显示用于获取
testing.q
初始值的代码。这在我的末端&var testing={q:“Hello World”}非常有效;testing.q=testing.q.split(“”).join(‘+’);根据错误,q没有被初始化。你确认q实际上说“你好,世界”了吗?更新了代码。我正在获取一个url参数。在第2行之后,
console.log(testing)
中有什么?@user10181542我认为您应该尝试不使用JSON.stringify()的代码,我在代码中尝试了JSON.stringify(),我得到了与您相同的错误。将给出错误:TypeError:如果在进一步测试后删除stringify(),则无法将对象转换为原始值,看起来parse stringify被调用了两次。第一次拆分没有遇到错误,第二次拆分给出类型错误。