0

I want to get datas from database (Mongodb) and write to EJS page.

My codes:

const express = require('express')
const router = express.Router()
const DroneSeries = require('../models/DroneSeries')

router.get('/technic-service', (req, res) => {

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

    console.log(droneSeries)

    res.render('technic-service', { droneSeries: droneSeries })
})

console.log(data) is works good. It gives me the data as I wanted (exp: all products). But if I write a code like console.log(droneSeries) then it gives me many many informations but not datas as I want(exp: hosting informations, user informations, connection information... but there are no products informations...). That's why I can't get data from droneSeries variable. So I can't render my data in EJS. How to render my products to EJS?

yusufcode
  • 391
  • 5
  • 19
  • put `res.render()` inside of your callback function. As your callback is executed asynchronously, your render function is being called before `droneSeriesData = data` is set – Nick Parsons May 17 '21 at 12:54
  • @NickParsons Can you write an example? – yusufcode May 17 '21 at 12:57
  • Instead of `droneSeriesData =. data`, use `res.render('technic-service', { droneSeries: data })` – Nick Parsons May 17 '21 at 12:58
  • @NickParsons data is not defined.... – yusufcode May 17 '21 at 13:01
  • If `data` is undefined then you have two issues, one with your async code, and another to do with your `data` being undefined. I suggest you update your question to remove the issue with the asynchronous code to highlight your real problem – Nick Parsons May 17 '21 at 13:09
  • If the linked question doesn’t answer your question, you can edit your question to say why, and then it might get re-opened. I already re-opened but another experienced community member has also closed your question again. Your question (seems) to have multiple problems with it, the first is solved in the link. If your issue persists, then you can try and make it clear that your issue isn’t to do with asynchronous code by editing it, or by posting a question with the proposed fix and with the problematic code – Nick Parsons May 17 '21 at 13:49
  • I edited the question, can you re-open it please? – yusufcode May 17 '21 at 14:21
  • Your issue is to do with the asynchronous nature of your code - your edit further highlights this. As I suggested before, you can move your `res.render()` into your callback, so instead of doing `console.log(data);` you can do `res.render('technic-service', { droneSeries: data })` _inside_ of your callback (since `data` holds the information you need). Accessing `data` outside of your callback can only be done with `async`/`await` (which doesn't need callbacks), but you don't need to do that here since you can just move `res.render()` inside of your callback – Nick Parsons May 17 '21 at 22:52
  • I solved the problem but I can't add answer... – yusufcode May 17 '21 at 23:07

0 Answers0