0

i want to display the data from two different mongoose collection.

I have two collection which is Member collection and Property collection

here is my get code

const Property  = require('../models/propsSchema')
const Members  = require('../models/userSchema')


router.get('/', (req, res, next) => {
  Members.find({})
  Property.find({})
  .exec()
  .then((props, member) => {
    console.log(props)
    console.log(member)
    res.render('index', { member : member, props : props } )    
    })
})

The console log result of "member" is undefined but the console log of "props" is the data of "Property". I want to fetch them both

1 Answers1

0

You can use Promise.all() function in your code

const Property  = require('../models/propsSchema')
const Members  = require('../models/userSchema')


router.get('/', (req, res, next) => {
  Promise.all([Members.find({}),
  Property.find({})])
  .then((data) => {
    console.log(data[0])
    console.log(data[1])
    res.render('index', { member : data[0], props : data[1] } )    
    })
})
Keren Caelen
  • 1,466
  • 3
  • 17
  • 38
Arif Khan
  • 5,039
  • 2
  • 16
  • 27