Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

81
Views
Convert MongoDB data to json on client side

I'm trying to convert mongoDB data passed by cookie on my client side from my server. I'm using Express et React.js.

Cient:

export default class Profile extends React.Component {

constructor(){
    super();
    this.state = {}
}

componentWillMount(){
    console.log(JSON.stringify(cookie.load('user')))
}

render(){
    return (
        <div>
            <h1>Profile</h1>
        </div>
    )
}

The console.log return :

"j:{\"_id\":\"58e622ac7144862dbb5722f1\",\"password\":\"paswdtest\",\"email\":\"test@test.com\",\"pseudo\":\"testname\",\"__v\":0}"

Server:

const post = (req, res, next) => {


if(req.body.pseudo && req.body.password) {
        User.authenticate(req.body.pseudo, req.body.password, (error, user) => {
            if(error || !user) {
                var error = new Error('Wrong email or password')
                error.status = 401;
                return next(error);
            }
            else {
                req.session.user = user;
                res.cookie('user', req.session.user)
                return res.redirect('/profile');
          }
     })
   }
   else {
        var error = new Error('Email and password are required');
       error.status = 401;
       return next(error);
   }
}

I'm trying to convert with parse and stringlify but it's not working.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

MongoDB doesn't return JSON but instead it returns extended JSON called BSON.

If your needs are simple then the quickest approach may be to just convert the BSON to JSON in your code.

For your example this would be

before

"j:{\"_id\":\"58e622ac7144862dbb5722f1\",\"password\":\"paswdtest\",\"email\":\"test@test.com\",\"pseudo\":\"testname\",\"__v\":0}"

after

{"j":{"_id":"58e622ac7144862dbb5722f1","password":"paswdtest","email":"test@test.com","pseudo":"testname","__v":0}}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!