Node js request module get different type (format) of return value from server?

Home / Uncategorized / Node js request module get different type (format) of return value from server?

Question:
I use request library to access web service, but I found that the return value can be different.
exports.getUserByEmail = function(req, res) {
request.get(endPoint + req.params.email, function(error, response, user) {
console.log((typeof user)) // type of user is string
… some logic
})
}

exports.login = function(req, res) {
request.post(endPoint + ‘login’, {json: { email: req.body.email, password: req.body.password }}, function(error, response, user) {
console.log((typeof user)) // type of user is object
… some logic
})
}

On the server side, it returns a JSON, e.g.
{
username: "user",
password: "hashedPassword",
……
}

Can anyone tell me why .get and .post method gives me different types of return value?


Answer:
I figured that out, sorry about this stupid question. Forgot to set {json:true} as option in get method.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *