我想将字符串转换为 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
行中看到错误消息时,请尝试使用 google 搜索该函数名称。
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
关于javascript - Nodejs- TypeError : JSON. Parse,JSON.Stringify 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771185/