Javascript 如何在Nodejs中使用.then()函数中的回调?

Javascript 如何在Nodejs中使用.then()函数中的回调?,javascript,node.js,mongodb,promise,callback,Javascript,Node.js,Mongodb,Promise,Callback,我使用nodejs模块使用mongodb驱动程序从mongodb数据库获取数据。回调函数被传递给给定的函数,该函数返回一个承诺,而不是返回结果。然后,它将值传递给回调函数。我如何从其他模块或函数调用此函数,因为它没有返回它。然后?我试图安慰的结果。然后,但它显示未定义 const MongoClient=require'mongodb'。MongoClient; const Db=require'../model/Db'; Db.findUser=详细信息,回调=>{ 返回dbconnecti

我使用nodejs模块使用mongodb驱动程序从mongodb数据库获取数据。回调函数被传递给给定的函数,该函数返回一个承诺,而不是返回结果。然后,它将值传递给回调函数。我如何从其他模块或函数调用此函数,因为它没有返回它。然后?我试图安慰的结果。然后,但它显示未定义

const MongoClient=require'mongodb'。MongoClient; const Db=require'../model/Db'; Db.findUser=详细信息,回调=>{ 返回dbconnection.thendb=>{ if-db{ 返回db.collection'users'。findOne{ 电子邮件:details.email, 密码:details.password }.thendata=>{ 如果数据{ console.log“找到一个”; 回调真; }否则{ 设err=新错误; callbackerr; } }
} 您可以使用async/await轻松完成此操作。类似以下内容:

Db.findUser = async (details, callback) => {
  const db = await dbconnection();
  const data = await db.collection('users').findOne({
    email: details.email,
    pass: details.password
  });

  if (data) {
    console.log('Found one');
    callback(true);
  } else {
    let err = new Error();
    callback(err);
  }

  return data;
}
然后像这样消费:

const getUser = async (details, callback) => {
  const data = await Db.findUser();

  // do whatever you need with data  

  return data;  
}