[英]Nodejs- TypeError: JSON.Parse,JSON.Stringfy is not a function
我想将字符串转换为JSON,反之亦然。 我在下面的router.js中使用JSON.Stringfy。 我是否需要在项目中使用JSON.Stringfy函数添加任何特定的npm?
以下是代码:
**app.js** file(main file executed by node.exe)
app.get('/about',routes.about);-->calling it using **http://localhost:3000/about**
**routers.js**
var User = {
'Name' :'',
'EmailId':'',
'Phone':'',
'Address':'',
'favouriteSport':''
}
exports.about = function(req,res){
res.send("Welcome to sports maniac" + JSON.Stringfy(User));
}
看到以下异常:
TypeError: JSON.Stringfy is not a function
at exports.about (F:\Personal Filess\Google Drive\Software Tools\Projects\routes\router.js:17:45)
at Layer.handle [as handle_request] (F:\Personal Filess\Google Drive\Software Tools\Projects\node_modules\express\lib\router\layer.js:95:5)
at next (F:\Personal Filess\Google Drive\Software Tools\Projects\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (F:\Personal Filess\Google Drive\Software Tools\Projects\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (F:\Personal Filess\Google Drive\Software Tools\Projects\node_modules\express\lib\router\layer.js:95:5)
at F:\Personal Filess\Google Drive\Software Tools\Projects\node_modules\express\lib\router\index.js:277:22
at Function.process_params (F:\Personal Filess\Google Drive\Software Tools\Projects\node_modules\express\lib\router\index.js:330:12)
at next (F:\Personal Filess\Google Drive\Software Tools\Projects\node_modules\express\lib\router\index.js:271:10)
at expressInit (F:\Personal Filess\Google Drive\Software Tools\Projects\node_modules\express\lib\middleware\init.js:33:5)
at Layer.handle [as handle_request] (F:\Personal Filess\Google Drive\Software Tools\Projects\node_modules\express\lib\router\layer.js:95:5)
它JSON.stringify
不是JSON.Stringfy
。
专业提示 - 下次你看到xyz is not a function
行上的错误消息xyz is not a function
尝试谷歌搜索该函数名称。
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
我认为你的函数名拼写错了,它是Stringify。
经过大量研究发现了JSON npm模块。 JSON2是我正在使用的npm模块。
只需在node.js命令提示符下运行命令:npm install json2 -g然后将节点模块添加到项目运行:npm install JSON2 --save
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.