MongoDB call inside feathers.js Hook

I want to get the info from a collection inside a feathers.js hook. How can i make the hook wait, until the mongodb call is complete? At the moment it sends the hook without waiting for the call to finish, i tried it with returns and promieses, but nothing worked

// Connection URL
const url = 'mongodb://localhost:27017/db';

//Use connect method to connect to the server

module.exports = function(hook) {
  MongoClient.connect(url, function(err, db) {
  const userCollection = db.collection('question');

  userCollection.count().then(function(N) {

    const R = Math.floor(Math.random() * N)

    const randomElement = userCollection.find().limit(1).skip(R).toArray(function(err, docs) {
    console.log("Found the following records");
    console.log(docs)
    //update hook with data from mongodb call
    hook.data.questionid = docs._id;
  });
  })
  })
};

Answers:

Answer

The ideal way is to make the hook asynchronous and return a Promise that resolved with the hook object:

// Connection URL
const url = 'mongodb://localhost:27017/db';
const connection = new Promise((resolve, reject) => {
  MongoClient.connect(url, function(err, db) {
    if(err) {
      return reject(err);
    }

    resolve(db);
  });
});

module.exports = function(hook) {
  return connection.then(db => {
      const userCollection = db.collection('question');
      return userCollection.count().then(function(N) {
        const R = Math.floor(Math.random() * N);

        return new Promise((resolve, reject) => {
          userCollection.find().limit(1)
            .skip(R).toArray(function(err, docs) {
              if(err) {
                return reject(err);
              }

              hook.data.questionid = docs._id;

              resolve(hook);
            });
        });
      });
    });
  });
};
Answer

The way to solve the thing is to use

module.exports = function(hook, next) {
    //insert your code
    userCollection.count().then(function(N) {
        const R = Math.floor(Math.random() * N)
        const randomElement = userCollection.find().limit(1).skip(R).toArray(function(err, docs) {
        console.log("Found the following records");
        hook.data.questionid = docs[0].email;
        //after all async calls, call next
        next();
      });

}
Answer

You can use async.waterfall() of async module

const async=require('async');

async.waterfall([function(callback) {
  userCollection.count().then(function(N) {
    callback(null, N);
  });
}, function(err, N) {
  if (!err) {
    const R = Math.floor(Math.random() * N)
    const randomElement = userCollection.find().limit(1).skip(R).toArray(function(err, docs) {
      console.log("Found the following records");
      console.log(docs)
        //update hook with data from mongodb call
      hook.data.questionid = docs._id;
    });
  }
}])
Answer

The solution of Daff didn't work for me. I got the following error:

info: TypeError: fn.bind is not a function

The solution was: It seems that normal hooks can be registered with brackets, but this hook has to be registered without brackets. findEnemy

exports.before = {
  all: [
    auth.verifyToken(),
    auth.populateUser(),
    auth.restrictToAuthenticated()],
  find: [],
  get: [],
  create: [findEnemy],
  update: [],
  patch: [],
  remove: []
};

findEnemy() does not work. Maybe others run into the same issue. Could someone explain why?

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.