【问题标题】:Nodejs- TypeError: JSON.Parse,JSON.Stringfy is not a functionNodejs- TypeError: JSON.Parse,JSON.Stringify 不是函数
【发布时间】:2016-02-19 15:57:31
【问题描述】:

我想将字符串转换为 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)

【问题讨论】:

    标签: javascript json node.js express npm


    【解决方案1】:

    我认为你的函数名拼写错误,它是 Stringify。

    【讨论】:

      【解决方案2】:

      它JSON.stringify 不是JSON.Stringfy。

      专业提示 - 下次您在 xyz is not a function 行中看到错误消息时,请尝试在谷歌上搜索该函数名称。

      https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

      【讨论】:

        【解决方案3】:

        经过大量研究发现 JSON npm 模块。 JSON2 是我正在使用的一个 npm 模块。

        只需在 node.js 命令提示符中运行命令:npm install json2 -g 然后将节点模块添加到项目运行: npm install JSON2 --save

        【讨论】:

        • 为什么要用 npm 处理 JSON?
        猜你喜欢
        • 2020-04-17
        • 1970-01-01
        • 2017-03-28
        • 2014-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-22
        • 2018-03-10
        相关资源
        最近更新 更多