admin管理员组

文章数量:1421751

I want to convert the string to JSON and vice versa. I am using JSON.Stringfy in router.js below. Do I need to have anything particular npm added to project use the JSON.Stringfy function?

Below is the code:

**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("Wele to sports maniac" + JSON.Stringfy(User));
}

seeing below exception:

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)

I want to convert the string to JSON and vice versa. I am using JSON.Stringfy in router.js below. Do I need to have anything particular npm added to project use the JSON.Stringfy function?

Below is the code:

**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("Wele to sports maniac" + JSON.Stringfy(User));
}

seeing below exception:

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)
Share Improve this question edited Nov 18, 2015 at 3:57 Swaraj Giri 4,0372 gold badges30 silver badges45 bronze badges asked Nov 18, 2015 at 3:13 Haridev NirgudeHaridev Nirgude 431 silver badge6 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2

ItJSON.stringify not JSON.Stringfy.

Pro tip - Next time you see a error message on the lines of xyz is not a function try googling that function name.

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

I think you have the function name spellt wrong, It's Stringify.

After a lot of research found the JSON npm module. JSON2 is a npm module which is I am using.

Just run the mand in node.js mand prompt :npm install json2 -g Then to add the node module to project run: npm install JSON2 --save

本文标签: javascriptNodejs TypeError JSONParseJSONStringfy is not a functionStack Overflow