Raul Contreras
Raul Contreras

Reputation: 29

Getting partial results using async.js mapValues

I was looking for a way to get results of mongoose js easy, simply by writing the query as you can see above:

Article.find().skip(data.skip)

And if i want to add another query, just i put the query and that is it:

Article.find().skip(data.skip)
Users.find().skip(data.skip),

So for get organization i put this queries into an object with her key:

Articles: Article.find().skip(data.skip),
Users: Users.find().skip(data.skip),

And next, i will be able to parse this with asyncjs by this way:

function process (obj, next) {
  console.log(obj.length);
  asyncjs.mapValues(obj, function(query, key, callback) {
    query.exec(function(err, results) {
      callback(results, key)
    });
  }, next)
}

So with this i expected that when next was called i would get the results as says in the asyncjs documentation:

async.mapValues({
    f1: 'file1',
    f2: 'file2',
    f3: 'file3'
}, function (file, key, callback) {
  fs.stat(file, callback);
}, function(err, result) {
    // result is now a map of stats for each file, e.g.
    // {
    //     f1: [stats for file1],
    //     f2: [stats for file2],
    //     f3: [stats for file3]
    // }
});

But that not was at that way, just i got the result of the first query, here is the full javascript sheet:

var mongoose = require('mongoose');
var asyncjs = require('async');
var app = require('express')();
mongoose.connect('mongodb://localhost/adifia');

mongoose.Promise = global.Promise;

var articlesScheme = new mongoose.Schema({
  title: String,
  state: String,
  content: String,
  author: String,
  permisionLVL: Number,
  created_at: Date
});
var usersScheme = new mongoose.Schema({
  name: String,
  email: String,
  password: String,
  age: Number,
  banned: Boolean
});

var Article = mongoose.model('Article', articlesScheme);
var Users = mongoose.model('User', usersScheme);

// Encontrar la menera de utilizando una funcion de async poder ejecutar dos consultas de mongoose y guardar los resultados en una variable
function process (obj, next) {
  console.log(obj.length);
  asyncjs.mapValues(obj, function(query, key, callback) {
    query.exec(function(err, results) {
      callback(results, key)
    });
  }, next)
}

function action(data, process, next) {
  var queries = {
    Articles: Article.find().skip(data.skip),
    Users: Users.find().skip(data.skip),
  };
  process(queries, next);
}

var query = {
  limit: 1
}

action(query, process, function(err, results) {
  console.log(err);
})

undefined
[ { _id: 58498848e938264fcfda298e,
    title: 'bieeen',
    content: 'yupiiiiaa',
    author: null,
    created_at: 2016-12-08T16:20:24.688Z,
    state: 'draft',
    __v: 0 },
  { _id: 58498848e938263fcfda298e,
    title: 'bien',
    content: 'yupiiaa',
    author: null,
    created_at: 2016-12-08T16:20:24.688Z,
    state: 'draft',
    __v: 0 } ]

Upvotes: 3

Views: 274

Answers (1)

GilZ
GilZ

Reputation: 6477

Note that the callback of async.mapValues expects to be called with the Node.js convention: err as the 1st argument, res as the second. You called it like so:

callback(results, key)

So, if results isn't falsey, async wouldn't call the your function on the next value. If there was no error, call callback with a falsey value (the convention is null)

Upvotes: 1

Related Questions