question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get access to data from Model.findOne() outside its body.

See original GitHub issue

Prerequisites

  • 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:closed
  • Created 9 months ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
Jacob1Tmcommented, Dec 18, 2022

and now i see what i did wrong thx

1reaction
Uzlopakcommented, Dec 17, 2022

Please first learn about async await and Promises, and how they prevent you from callback hell.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found