Tag: json

Use request module to retrieve JSON and return value

I use this request module to make HTTP request in Nodejs Sample code: module.exports.getToken = function(){ var token ; request(validLoginRequest, function(err,resp,body){ var json = JSON.parse(JSON.stringify(body)); console.log(“from request(): token=” + json.accesstoken); token = json.accesstoken; }); console.log(“getToken() returns:” + token); return token; } But the token is always undefined. What did I do wrong?

JSON stringify in Node JS not serializing array of objects

I am using sails.js (node js framework). I am trying to JSON.stringify one of the objects, but when I do that it omits one of the fields (rooms array below). Here is what console.log(object) gives me: [ { rooms: [ [Object], [Object] ], state: ’53df76c278999310248072c6′, name: ‘Sydney Center’, menuItems: null, createdAt: Mon Aug 04 2014 […]

node and express send json formatted

I’m trying to send formatted json with express. Here is my code: var app = express(); app.get(‘/’, function (req, res) { users.find({}).toArray(function(err, results){ // I have try both res.send(JSON.stringify(results, null, 4)); // OR res.json(results); }); }); I get the json in my browser but it’s a string. How can I send it so it’s readable […]

npm request not getting json response when doing body.{object}

So I am making request to twitch to get some streamer data using npm response. var express = require(‘express’); var router = express.Router(); var request = require(‘request’); /* GET users listing. */ router.get(‘/streams/:user’, function(req, res, next) { request(‘https://api.twitch.tv/kraken/streams/’ + req.params.user, function ( error, response, body) { if (!error && response.statusCode == 200) { res.send(body); } […]

sequelize.query() returns same result twice

I am working in nodejs project in that using sequelize for connecting mysql database. I am also using sequelize-values for getting raw data from Sequelize instances. I have written below code var Sequelize = require(‘sequelize’); require(‘sequelize-values’)(Sequelize); var sequelizeObj = new Sequelize(‘mysql://root:@localhost/database’); sequelizeObj.authenticate().then(function (errors) { console.log(errors) }); sequelizeObj.query(“SELECT * FROM `reports` WHERE `id` = 1200”).then(function (result) […]

NodeJS write log file (Json)

I want to write logs into a Json file newEntry = “User: ” + lastUsername + ” Time: “+now+ ” Door: “+IOSDoor; lastUserOpenClose += newEntry; jsonString = JSON.stringify(lastUserOpenClose); fs.writeFile(“lastUserOpenClose.json”, lastUserOpenClose, function(err) { if(err) { console.log(err); } else { console.log(“Server-Log: The file was saved!”); } }); But I’m overwriting the logs. How to write somthing into […]

Parsing JSON in Express without BodyParser

I’m trying to write a simple express server that takes incoming JSON (POST), parses the JSON and assigns to the request body. The catch is I cannot use bodyparser. Below is my server with a simple middleware function being passed to app.use Problem: whenever I send dummy POST requests to my server with superagent (npm […]

How to add proxy to Node / Express site?

My site is running on Node and using the Express framework. My goal is to gather data from the Yahoo Placefinder api. It does not support JSONP, so I need to send my JQuery.getJSON request to my own proxy. My proxy would then send an http request to the Placefinder api, and echo the response. […]

Mongoose schema for storing an array of JSON objects for subdocuments

I have a JSON object that I want to create a schema for using mongoose { ProjectName: ‘asdf’, Embargo: ‘Yes’, Angle: ‘1’, Facts: ‘[{“count”:1,”fact”:”dsafdsaf”,”content”:”dsafdsaf”}, {“count”:3,”fact”:”dsafdsaf”,”content”:”dsafdsaf” } , {“count”:2,”fact”:”dsafdsaf”,”content”:”dsafdsaf”}]’, About: ‘<p>Anthony Bodin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>’, EditorNote: ‘No’, OrderId: ‘tok_14kGRO2Jju1nvjb47YF9jQTJ’, Payment: ‘Paid’ } My problem is that the Facts element will contain and array of objects and I am not […]

json returned from flickr api not getting parsed

I am trying to consume flickr api and requesting data in json format from a node.js server. I am providing nojsoncallback=1 so that returned json is not wrapped inside a jsonp callback. Here is the url http://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1. Json returned is not properly formatted and JSON.parse is unable to parse it. If I don’t pass nojsoncallback […]

Node.js is the Best Javascript runtime in the world.