const ejs = require('ejs')
const path = require('path');
const fastify = require('fastify')();
const fastify_static = require('fastify-static');
const fastify_autoload = require('fastify-autoload');
const fastify_mongoose = require('fastify-mongoose');
const fastify_env = require('fastify-env');
let PORT;
let uri;
fastify.register(fastify_env, {
dotenv: true,
schema: {
type: 'object',
required: ['MONGO_URI', 'PORT'],
properties: {
MONGO_URI: {
type: 'string',
default: ''
},
PORT: {
type: 'string',
default: ''
}
}
}
}).ready(err => {
if (err) {
throw new Error(err);
}
PORT = fastify.config.PORT;
uri = fastify.config.MONGO_URI;
})
fastify.register(fastify_static, {
root: path.join(__dirname, 'public'),
})
fastify.register(require('point-of-view'), {
engine: {
ejs: ejs,
},
root: path.join(__dirname, 'view')
})
fastify.register(fastify_autoload, {
dir: path.join(__dirname, 'Logic/Routes'),
})
fastify.register(require('./Logic/Plugins/cache'))
fastify.register(fastify_mongoose, {
uri: uri
})
const start = async () => {
try {
await fastify.ready();
await fastify.listen(process.env.PORT);
console.log('Listening on port: ', PORT);
} catch (error) {
throw new Error(error);
}
}
start();
Error: Error: uri parameter is mandatory
at D:\Users\Antonio\Desktop\Antonio\Work\GitHub\NextLevel\src\main.js:30:15
The problem is with fastify-mongoose, or probably with fastify-env. I explain: if i write directly the URI in the URI parameter, it works. But using environment variables no..
Are you using dotenv, because you should, I don't advise you to put passwords in your code or access to your APIs.
require('dotenv').config()
let's remember that when we started
start()
we starting fastify and dotenv has to be invoked, otherwise process.env.YOUR_VARIABLE will not be included