Tuesday 7 February 2017

How to display mongodb document values from a collection using Express?

I have documents in a collection eg. below, and all I simply want to do is display JSON on the web page. I also want to be able to do something like console.log(docs[1].name) or however to display the first documents name value. { "_id" : ObjectId("5897c4283764b39861b88c1c"), "photo" : "http://ift.tt/2loXgMA", "name" : "Mosel Slate", "estate" : "Mönchhof", "estate_link" : "http://ift.tt/2liP6bT", "region" : "Mosel-Saar-Ruwer", "country" : "germany", "vineyard" : "Ürziger Würzgarten", "style" : "40% Dry, 60% Sweet", "bubbly" : "bubbles", "soil_type" : "Devonian Red Slate", "imported_quantity" : "5,000 cases", "UPC_code" : "7 67946 10102 3", "fermentation_notes" : "lorem ipsum", "aging" : "lorem ipsum", "vine_age" : "lorem ipsum", "farming_practicies" : "lorem ipsum", "label_photo" : "http://ift.tt/2loO6jh", "vintage" : "2012", "varietal" : "100% Riesling", "alcohol" : "8.5%", "testing_notes" : "lorem ipsum", "awards" : " lorem ipsum", "achohol_vs_acidity" : "http://ift.tt/2liNdfd" } { "_id" : ObjectId("5897c5a53764b39861b88c1d"), "photo" : "http://ift.tt/2loQcQ5", "name" : "Riesling Piesport", "estate" : "Reinhold Haart", "estate_link" : "http://ift.tt/2liP6bT", "region" : "Mosel-Saar-Ruwer", "country" : "germany", "vineyard" : "Ürziger Würzgarten", "style" : "40% Dry, 60% Sweet", "bubbly" : "bubbles", "soil_type" : "Decomposed Slate and Kaolinite", "imported_quantity" : "5,000 cases", "UPC_code" : "7 67946 10900 5", "fermentation_notes" : "lorem ipsum", "aging" : "lorem ipsum", "vine_age" : "lorem ipsum", "farming_practicies" : "lorem ipsum", "label_photo" : "http://ift.tt/2liDd5x", "vintage" : "2011", "varietal" : "100% Riesling", "alcohol" : "8.5%", "testing_notes" : "lorem ipsum", "awards" : " lorem ipsum", "achohol_vs_acidity" : "http://ift.tt/2liNdfd" } In my express app I try:router.get('/wineslist.json', function(req, res) { var db = req.db; var collection = db.get('wines'); var wines = collection.find(function(e, docs){ res.send(docs); }); }); I then get in result:/usr/local/lib/node_modules/mongodb/lib/utils.js:98 process.nextTick(function() { throw err; }); ^ TypeError: Converting circular structure to JSON so I then try to console.log(data) to see what it is and I get this which I have clue what it is (snippet)Readable { pool: null, server: null, disconnectHandler: { s: { storedOps: [], storeOptions: [Object], topology: [Object] }, length: [Getter] }, bson: {}, ns: 'hirschdb.wines', cmd: { find: 'hirschdb.wines', limit: 0, skip: 0, query: {}, fields: {}, sort: {}, safe: true, slaveOk: true, readPreference: { preference: 'primary', tags: undefined, options: [Object] } }, options: { fields: {}, sort: {}, safe: true, skip: 0, limit: 0, raw: undefined, hint: null, timeout: undefined, slaveOk: true, readPreference: { preference: 'primary', tags: undefined, options: [Object] }, db: EventEmitter { domain: null, _events: {}, _eventsCount: 0, _maxListeners: undefined, s: [Object], serverConfig: [Getter], bufferMaxEntries: [Getter], databaseName: [Getter] }, promiseLibrary: [Function: Promise], disconnectHandler: { s: [Object], length: [Getter] } }, topology:

Submitted February 08, 2017 at 05:10AM by mineralwatersoda

No comments:

Post a Comment