I have read many articles relating my issue such as (Returning a variable from a function in an imported module and NodeJS can't returned data from imported module is undefined) I also read (Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference and How do I return the response from an asynchronous call?) but im still stuck, I tried all the solutions by my issue is still persistent.
I am new to JS and am new to promises and await, which is mainly why I am confused. I am using a simple mongoose function,
//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);
}
}
The function finds a user based on their name and returns it, in the local file I can access the function return value like this
foundUserObject = await findUser(associatedUser);
and it works.
However when I import the module, and give an input
const MongoDBModule = require('./index');
MongoDBModule.findUser(associatedUserPrompt)
It always returns undefined, when I try to make my return a promise in the original code
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(FoundUserObject);
}, 5000);
});
which is what some links told me to do I still get undefined; I think its because im returning a static value and not a request but im not too sure
When I try to put the return in an async function I get this error 'await' has no effect on the type of this expression
and when I try to use
MongoDBModule.findUser(associatedUserPrompt).then(value => {
console.log(value);
});
I get
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
From what I read this has something to do with a synchronous function trying to access an asynchronous return function, and I read many posts and tried everything but I'm really stuck. I just need to know how to not return undefined and return the object which I'm supposed to be getting.
This is what I'm supposed to get --
{
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
}
How I export the function
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);
},
};