0

I got my node.js server to output MongoDb data in the browser like this

"[
  {"_id":"5575532d38957e14136ac889","masa":"Masa 1","statut":"nou","produse":[]},   
  {"_id":"55755428809ca96419c296ae","masa":"Masa 5","statut":"nou","produse":    
  [{"numeprod":"Espresso","cantitate":"1","pret":"10"},
   {"numeprod":"Late","cantitate":"1","pret":"15"}]},   
   {"_id":"557553ec38957e14136ac88a","masa":"Masa 2","statut":"nou","produse":
   [{"numeprod":"Briose","cantitate":"19","pret":"15"},  
    {"numeprod":"Late","cantitate":"1","pret":"15"},
    {"numeprod":"Espresso","cantitate":"1","pret":"10"},
    {"numeprod":"URSUS","cantitate":"4","pret":"5"}
   ]}
]"

How can i decode this JSON data and display it in a HTML table ? I'm new to this , and reading other questions on this are not helping Thank You

1
  • If you using express with ejs/jade, you response the JSON data and display in a common HTML table. Commented Jun 9, 2015 at 11:38

1 Answer 1

1

In order to display the data, you have to define the data for the view you want to use it for. Instead of requesting the raw data (what you're getting), store the data in a variable and pass it into a structured html page:

server.js:

var http = require('http');
var file = require('file');
var middleware = function(req, res) {
    res.send(file);
    res.end();
};
http.createServer(middleware).listen(somePort);

file.js

var db = require('mongodb').MongoClient; // I assume you're using node-mongodb-native
db.connect(mongoURLtoDb, function(err, db) {
    db.collection(yourcollection).find().toArray(function(err, data) {
        var output = "
            //Here you can put whatever HTML markup you want. 
            " + data.key1 + "
            // etc...
        ";
        module.exports = output;
    });
});
Sign up to request clarification or add additional context in comments.

Comments

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.