mongoose 从异步导入模块返回数据

bq9c1y66  于 2023-02-19  发布在  Go
关注(0)|答案(1)|浏览(122)

我已经读了很多关于我的问题的文章,如(Returning a variable from a function in an imported moduleNodeJS can't returned data from imported module is undefined)我也读了(Why is my variable unaltered after I modify it inside of a function? - Asynchronous code referenceHow do I return the response from an asynchronous call?),但我仍然卡住了,我尝试了所有的解决方案,我的问题仍然存在。
我是js的新手,也是promises和waiting的新手,这也是我感到困惑的主要原因,我正在使用一个简单的 Mongoose 函数,

//Find and return user object function
async function findUser(associatedUser) {
  try {
    //Find a user based on given info
    const foundUser = await User.find({ associatedUser: associatedUser });
    //It returns a list of object, but usually its just one object. So we just get that one object
    const foundUserObject = foundUser.find(o => o.associatedUser == associatedUser);
    return foundUserObject;
  } catch (err) {
    console.log(err);
    process.exit(1);
  }
}

函数根据用户名查找用户并返回,在本地文件中,我可以访问函数返回值,如下所示

foundUserObject = await findUser(associatedUser);

而且很有效。
但是当我导入模块,并给出一个输入

const MongoDBModule = require('./index');

MongoDBModule.findUser(associatedUserPrompt)

当我试图在原始代码中使返回成为一个承诺时,它总是返回undefined

return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(FoundUserObject);
        }, 5000);
    });

这是一些链接告诉我要做的,我仍然没有定义;我想这是因为我返回的是静态值而不是请求,但我不太确定
当我尝试将返回值放入异步函数时,我得到错误'await' has no effect on the type of this expression
当我尝试使用

MongoDBModule.findUser(associatedUserPrompt).then(value => {
  console.log(value);
});

我得到

TypeError: Cannot read properties of undefined (reading 'then')
    at Object.<anonymous> (D:\Programming Related\MongooseTests\testingimport.js:18:45)  
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.14.0

从我读到的内容来看,这与一个同步函数试图访问一个异步返回函数有关,我读了很多帖子,尝试了所有方法,但我真的卡住了。我只需要知道如何不返回未定义的,并返回我应该得到的对象。
这是我应该得到的

{
  associatedUser: '971567800902@c.us',
  currentContext: 'none',
  name: 'none',
  email: 'none',
  subject: 'none',
  budget: 'none',
  phone: 'none',
  message: 'none',
  mailinglist: false,
  _id: new ObjectId("63efda10621fb2247732d115"),
  __v: 0
}

如何导出函数

module.exports = {
  findUserAndUpdateValues: function (associatedUser, givenObjectValue, newValue) {
    findUserAndUpdateValues(associatedUser, givenObjectValue, newValue);
  },

  createUser: function (associatedUser, currentContext, name, email, subject, budget, phone, message, mailinglist) {
    createUser(associatedUser, currentContext, name, email, subject, budget, phone, message, mailinglist);
  },

  findUser: function (associatedUser) {
    findUser(associatedUser);
  },
};
polhcujo

polhcujo1#

添加缺少的return

module.exports = {
  // ...
  findUser: function (associatedUser) {
    return findUser(associatedUser);
  },
};

或者根本不创建 Package 函数

module.exports = {
  // ...
  findUser
};

相关问题