apache / nano

Nano is now part of Apache CouchDB. Repo moved to https://GitHub.com/apache/couchdb-nano
https://github.com/apache/couchdb-nano
Other
1.13k stars 157 forks source link

Some question... #316

Open ghost opened 8 years ago

ghost commented 8 years ago

I wanted to get [{id: 'apples', name: 'Apples'}, {id: 'pears', name: 'Pears'}] as output, but I get [] as output.

I added the documents to the database with Futon like this:

This is my code:

var nano = require('nano')('http://localhost:5984');

var categories = nano.db.use('categories');

var listCategories = callback => {
  if (callback) {
    categories.list(function(err, body) {
      if (!err) {
        var toReturn = [];
        body.rows.forEach(function(doc) {
          toReturn.push(doc.id);
        });
        callback(toReturn);
      }
    });
  }
};

var getCategoryName = (category, callback) => {
  if (category && callback) {
    categories.get(category, {revs_info: false}, function(err, body) {
      if (!err) {
        callback(body.name);
      }
    });
  }
};

var listCategoriesWithNames = callback => {
  if (callback) {
    listCategories(function(cats) {
      var toReturn = [];
      cats.forEach(function(cat) {
        getCategoryName(cat, function(name) {
          var temp = {};
          temp[cat] = name;
          toReturn.push(temp);
        });
      });
      callback(toReturn);
    })
  }
};

listCategoriesWithNames(function(returned) {
  console.log(returned);
});

So, how do I get the desired output?

carlosduclos commented 7 years ago

This repository has been merged into apache/couchdb-nano, please continue the discussion here