1

I want to get value of callback function.

This is the DroneSeries Model:

const mongoose = require('mongoose')
const Schema = new mongoose.Schema({
    name: {type:String},
    cat: {type:Number}
}, {timestamps:true})

module.exports = mongoose.model('droneseries', Schema)

This is my code:

const DroneSeries = require('../models/DroneSeries')
const droneSeries = DroneSeries.find((err, data) => {
    if (err) {
        console.log('Datas couldn\'t get ', err)
    } else {
        return data
    }
})

How to get data to console.log?

console.log(droneSeries.data)

When I try this I get the "Undefined" error... How can I get it?

yusufcode
  • 391
  • 5
  • 19
  • Can you add the array structure of `data`? – Anand G May 17 '21 at 14:48
  • Please see [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992), which this duplicates. – Dave Newton May 17 '21 at 14:48
  • @AnandG I added the model codes. – yusufcode May 17 '21 at 14:50
  • I don't think `find` would return you any error if that `err` is an error you are expecting. – Anand G May 17 '21 at 14:55
  • @AnandG so, how can I get the value of data callback? – yusufcode May 17 '21 at 14:57
  • Please see [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992), which this duplicates. – Dave Newton May 17 '21 at 14:58
  • 1
    @DaveNewton How does this relates with your link shared? OP concerns is not about Async call. The way he is expecting `find` to behave is not correct. I don;t this question should be closed. – Anand G May 17 '21 at 15:01
  • @AnandG They are always acting like this... Close the quesiton... This is a duplicate question... – yusufcode May 17 '21 at 15:05
  • @AnandG `find` is async. This is not how you get the results of an async call. – Dave Newton May 17 '21 at 15:05
  • @yusufcode When it's a dupe, yes. `droneSeries` does not contain the `data` returned by `find`, because `find` is an async call. – Dave Newton May 17 '21 at 15:06
  • 1
    @yusufcode There already *is* an answer to this question, in the duplicate. This is how SO works: instead of duplicating answers we point to the duplicate. This reduces noise and confusion. If you feel it *isn't* a dupe you can update the question with *why* it isn't and vote to re-open, or take it up on [meta](https://meta.stackoverflow.com/). – Dave Newton May 17 '21 at 15:08
  • @yusufcode https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find this could help you to find what is wrong there. – Anand G May 17 '21 at 15:24
  • @AnandG DroneSeries is a Mongo model, not an array. – Dave Newton May 17 '21 at 15:39

0 Answers0