1

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("Welcome 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)
0

3 Answers 3

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.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

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

1 Comment

Why you want npm for JSON?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.