0

First of all, i want to let you know that i am a novice with node and couchDB and i have this project where i need to add some functionality to the existing application.

So, i have javascript/node/express web application and i want to get a specific document from a remote couchDB, using cradle, and then get a list of its fields and their values. Later on i would need to display those fields/values in some html. I don't know which fields the document has because they are dynamically added/removed by a third party. I was able to get the document i wanted, but i don't know how to iterate through its fields. What would be the best way to do that?

Here is a simpificated sample of the document:

{
"_id": "1.1.5",
"_rev": "5-56ebac233e7f56a14a4534c6902727f7",
"1.1.5.39": {
   "Project": {
       "Project1": {
           "files": "...",
           "status": "NEW",
           "id": 2
       },
       "Project2": {
           "files": "...",
           "status": "ASSIGNED",
           "id": 3
       }
   }
}
"1.1.5.23": {
   "Project": {
       "Project3": {
           "files": "...",
           "status": "NEW",
           "id": 4
       },
       "Project4": {
           "files": "...",
           "status": "NEW",
           "id": 5
       }
   }
}
}

I would need to get the fields '1.1.5.39' and '1.1.5.23', and also the 'status' values. These fields represent some versions of a software. The problem is also fields' format: numbers and dots, so i can't just use 'Object.attribute' notation...

Basic
  • 41
  • 5
  • Every time use cradle a cute little kitten dies. – dscape Sep 14 '12 at 03:32
  • 2
    Since you're using node [this technique](http://stackoverflow.com/questions/921789/how-to-loop-through-javascript-object-literal-with-objects-as-members) applies. – lambmj Sep 14 '12 at 17:45

1 Answers1

0

First part is getting the document via cradle:

// get the Connection
var connection = new(cradle.Connection)('http://living-room.couch', 5984, {
  cache: true,
  raw: false
});
// get the DB
var db = connection.db('yourDB');

// get the document
var docID = "1.1.5";
db.get('docID', function (err, doc) {
  displayDoc(doc);
});

function displayDoc(doc) {
  // Do your display handling here
}

The second part what you need is displaying arbitrary objects. Here I refer you to my answer to this question.

Good Luck.

Community
  • 1
  • 1
dignifiedquire
  • 1,360
  • 9
  • 14