MongoDB calls inside feathers.js hook

I want to get information from the collection inside the feathers.js hook. How can I wait until the mongodb call completes? At the moment, it sends a hook, without waiting for the call to end, 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;
  });
  })
  })
};
Run codeHide result
+4
source share
4 answers

The ideal way is to make it asynchronous and return the Promise , which is allowed using 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);
            });
        });
      });
    });
  });
};
+3
source

The way to solve this problem 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();
      });

}
+1
source

async.waterfall()

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;
    });
  }
}])
0

. :

info: TypeError: fn.bind is not a function

The solution was: It seems that ordinary hooks can be registered in brackets, but this hook should 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. Perhaps others are facing the same issue. Can someone explain why?

0
source

Source: https://habr.com/ru/post/1655949/


All Articles