Can't get access to data from Model.findOne() outside its body.
See original GitHub issuePrerequisites
- I have written a descriptive issue title
Mongoose version
6.6.0
Node.js version
18.8.0
MongoDB version
6.0
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
11
Issue
So im trying to get the data from my db using moongose like that
let value;
Model.findOne({ something: something}, (err, Doc) => {
if (err) {
console.error(err);
return;
}
if (Doc) {
value = Doc.value
} else {
value = config.value
}
});
but i need to get it in entire program and i don’t really know how to do it. when i try to console log in this i have a correct value, but outside of it i get undefined.
Issue Analytics
- State:
- Created 9 months ago
- Comments:11
Top Results From Across the Web
Mongoose TypeError on a model's findOne method
1 Answer 1 ·./user.js is where the UserModel is defined. · remove the require, and change UserModel.findOne() to mongoose. · Have you defined ......
Read more >sequelize.findOne seems to be broken. · Issue #9990 - GitHub
findOne is returning undefined, despite the output showing a correct query string. Output, either JSON or SQL C:\RengTech\Coconut>node server.js
Read more >06 - Use model.findOne() to Return a Single Document
If we are just looking for one result, we can call the findOne () method on a model. Once again we give an...
Read more >1. Working with Spring Data Repositories
USE_DECLARED_QUERY tries to find a declared query and will throw an exception in case it can't find one. The query can be defined...
Read more >Get API | Elasticsearch Guide [8.5] | Elastic
You use GET to retrieve a document and its source or stored fields from a particular index. Use HEAD to verify that a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
and now i see what i did wrong thxPlease first learn about async await and Promises, and how they prevent you from callback hell.