delay between my function and the redirection to a route

delay between my function and the redirection to a route

I'm working on a page in nodeJs with express which return me a table. I have an error about my variable "allMusique" containing my data on my page when I try to access it but after I reload once, the page shows up correctly with my data. Does someone see what is wrong please?

I'm working on a page in nodeJs with express which return me a table. I have an error about my variable "allMusique" containing my data on my page when I try to access it but after I reload once, the page shows up correctly with my data. Does someone see what is wrong please?

Here is my code:

var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
var allMusique;
var idMaxMusique;

router.get('/', function(req, res, next) { function getAllMusique(){ MongoClient.connect(url, function(err, db) { if (err) throw err; var dbo = db.db("projet_node"); dbo.collection("musiques").find({}).toArray(function(err, result) { if (err) throw err; allMusique = result; var size = allMusique.length-1; idMaxMusique = parseInt(result[size].id)+1; }); }); }

getAllMusique();
res.render('musiques', { resultat: allMusique, idMax: idMaxMusique });

});

module.exports = router;


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to Use Express.js, Node.js and MongoDB.js

In this post, I will show you how to use Express.js, Node.js and MongoDB.js. We will be creating a very simple Node application, that will allow users to input data that they want to store in a MongoDB database. It will also show all items that have been entered into the database.

Node.js Express FrameWork Tutorial - Learn Express.js in 15 minutes

Express JS is an awesome opinionated framework for Node.js that helps you create REST end points. Let's learn Express.js in 15 minutes! - Node.js Express FrameWork Tutorial. This framework is built in such a way that it acts as a minimal and flexible Node.js web application framework, providing a robust set of features for building single and multipage, and hybrid web application.

Secure Node.js, Express.js and PostgreSQL API using Passport.js

The comprehensive step by step tutorial on building secure Node.js, Express.js, Passport.js, and PostgreSQL Restful Web Service