Considering the following code:
db.js
// Connecting to catalogstore (mongodb)
const mydb = async () => {
try {
await mongoose.connect(process.env.db);
console.log("Connected to Database!");
}
catch (err) {
throw new Error("Database connection error:", err);
}
};
export { db }
app.js
import { db } from './db';
db().then(async() => {
try {
let server = app.listen(process.env.port,
process.env.host, function() {
let host = server.address().address;
let port = server.address().port;
console.log('App started');
});
} catch (err) {
console.log(err);
}
});
Basically I'd like to start an Express server only after establishing the db connection.
It actually works fine, however I get this warning:
(node:29892) UnhandledPromiseRejectionWarning: Error: Database connection error:
at catalogstore (/Users/notaris/Workspace/Google/gcp-devops/apps/catalogservice/src/db.js:44:11)
at processTicksAndRejections (internal/process/task_queues.js:89:5)
(node:29892) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:29892) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
How can I properly handle that?
The error is thrown by the db async function.
The correct way to handle this Error (using an async function/arrow) is:
import { db } from './db';
const main = async () => {
try {
await db();
let server = app.listen(process.env.port,
process.env.host, function() {
let host = server.address().address;
let port = server.address().port;
console.log('App started');
});
} catch (err) {
console.log(err);
}
});
main();
Modify app.js like follow
import { db } from './db';
db()
.then(/* normal logic */)
.catch(/* error logic */)
Just catch it ^^
In debugger i see that you should catch the error in the promise like this :
//app.js
import { db } from './db';
db().then(async() => {
try {
let server = app.listen(process.env.port,
process.env.host, function() {
let host = server.address().address;
let port = server.address().port;
console.log('App started');
});
} catch (err) {
console.log(err);
}
}).catch(error=>{
console.log('error'+error);
});
And you can check this link for more details.
Related
New to node- I'm trying to save some of my tweets from Twitter API into mongo using Twit package.
I've connected to mongodb on port 27017 using mongoose, and this piece of code I've written seems to save the tweets to my db, however I seem to be getting this warning back everytime I save a document:
(node:9991) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 8)
Here is my code:
const Tweet = require('./app/models/tweet.model.js');
const dbConfig = require('./config/database.config.js');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(dbConfig.url, {
useNewUrlParser: true
}).then(() => {
console.log("Successfully connected to the database");
}).catch(err => {
console.log('Could not connect to the database. Exiting now...', err);
process.exit();
});
var Twit = require("twit");
var config = require("./config/twitter.config");
var T = new Twit(config);
var params = {
screen_name: "decade3uk",
count: 2
};
T.get("statuses/user_timeline", params, gotData);
function gotData(err, data, response) {
var tweets = data;
for(var i=0;i<tweets.length;i++){
const tweet = new Tweet({
created_at:tweets[i].created_at,
id_str:tweets[i].id_str,
text:tweets[i].text
});
tweet.save()
.then(entry => {
response.send(entry);
}).catch(err => {
response.status(500).send({
message: err.message || "Some error occurred while creating the Tweet."
});
});
}
}
What is best practice to get rid of this error?
Why don't you try to find where is that exception coming from and what exactly it is. You can find that by adding the following code to your server file, just to make sure you get what's causing the exception.
process.on('unhandledRejection', (reason, promise) => {
console.log("Reason: ",reason,"promise: ",promise);
})
Fastify changed a lot since the last time I played with it. Also, I read that we should use Nuxt serverMiddleware to add api routes but I couldn't find any example on how to proceed.
Below there is my index file to start Fastify. I tried adding a route in it before nuxt with this line:
fastify.register(require('./routesIndex'), { prefix: '/api' })
routesIndex.js as per the new fastify documentation:
async function routes(fastify, options) {
fastify.register(require('./fetchRemote'))
fastify.register(require('./domains'))
}
module.exports = routes
To be thorough, the domains.js file:
async function routes(fastify, options) {
const database = fastify.mongo.db('db')
const collection = database.collection('test')
fastify.get('/domains/list', async (request, reply) => {
return { hello: 'world' }
})
fastify.get('/domains/:id', async (request, reply) => {
const result = await collection.findOne({ id: request.params.id })
if (result.value === null) {
throw new Error('Invalid value')
}
return result.value
})
}
module.exports = routes
server/index.js:
const { Nuxt, Builder } = require('nuxt')
const fastify = require('fastify')({
logger: true
})
fastify.register(require('./db'), {
url: 'mongodb://localhost:27017'
})
// Import and Set Nuxt.js options
const config = require('../nuxt.config.js')
config.dev = !(process.env.NODE_ENV === 'production')
async function start() {
// Instantiate nuxt.js
const nuxt = new Nuxt(config)
const {
host = process.env.HOST || '127.0.0.1',
port = process.env.PORT || 3000
} = nuxt.options.server
// Build only in dev mode
if (config.dev) {
const builder = new Builder(nuxt)
await builder.build()
} else {
await nuxt.ready()
}
fastify.use(nuxt.render)
fastify.listen(port, host, (err, address) => {
if (err) {
fastify.log.error(err)
process.exit(1)
}
})
}
start()
The /api route is not found in the browser. But then I read I should use serverMiddleware inside nuxt.config.js:
serverMiddleware: [
{ path: '/api', handler: '~/server/routesIndex.js' },
],
Doing so results in an error from fastify.register:
(node:10441) UnhandledPromiseRejectionWarning: TypeError: fastify.register is not a function
at routes (/media/srv/testingNuxt/server/routesIndex.js:4:13)
at call (/media/srv/testingNuxt/node_modules/connect/index.js:239:7)
at next (/media/srv/testingNuxt/node_modules/connect/index.js:183:5)
at next (/media/srv/testingNuxt/node_modules/connect/index.js:161:14)
at WebpackBundler.middleware (/media/srv/testingNuxt/node_modules/#nuxt/webpack/dist/webpack.js:5430:5)
(node:10441) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10441) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
How can I add api routes using Fastify and Nuxt?
I want to avoid creating a second server on port 3001 just for the api.
node js thunder when I send multiple requests to the server at the same time, but not when I send one by one. And nodejs says.
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
(node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)
That error in common when you send two responses to the client but in my code only sent one
This is my code
const User = require('../../../modelos/users');
async function getAllValuationsByUserName(req, res, next) {
let isOwner = false;
const userId = req.params.id;
const pagination = {
skip: Number(req.query.skip),
limit: Number(req.query.limit)
}
if (userId === res.locals.userid) {
isOwner = true;
}
try {
const user = await User.findOne(
{ userName: new RegExp('^' + userId + '$', "i") },
{
'userPhoto.valuations': {
$slice: [pagination.skip, pagination.limit]
}
})
const valuations = user.userPhoto.valuations
const total = user.userPhoto.valuations.length
return res.status(200).send({
erro: false,
isOwner,
valuations,
total
})
} catch (err) {
console.log(err)
return res.status(400).send({
error: true,
inf: err
});
}
}
module.exports = getAllValuationsByUserName
I get these cryptic lines here:
DEBUG: Mongoose connected (node:5983)
UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 1): TypeError: Cannot read property 'then' of undefined
(node:5983) [DEP0018] DeprecationWarning: Unhandled promise rejections
are deprecated. In the future, promise rejections that are not handled
will terminate the Node.js process with a non-zero exit code.
How can I get useful debugging information so I don't have to guess where the exact issue is?
I believe it is somewhere in this file:
const mongoose = require('mongoose');
const helper = require('../config/helper');
const schema = require('./Schemas')
mongoose.connect(helper.getMongoose()).then(
() => {
console.log('DEBUG: Mongoose connected')
mongooseConnected();
},
(err) => {
console.log('DEBUG: Mongoose did not connect')
}
);
function mongooseConnected () {
makeSchema( schema.User,
{ id_google: '1',
type: 'person',
timestamp: Date.now()
});
}
function makeSchema (Schema, dataObj) {
const Class = mongoose.model('Class', Schema);
const Instance = new Class(dataObj);
Instance.save((err, results)=>{
if (err) {
return console.error(err);
}
}).then(() => {
console.log('Saved Successfully')
});
}
In your case you are providing a callback to your save function, this way mongoose will not return a Promise:
Instance.save((err, results)=>{
if (err) {
return console.error(err);
}
console.log('Saved Successfully')
})
If you still want to use Promise then you don't have to pass a callback function:
Instance.save().then(() => {
console.log('Saved Successfully')
}).catch(err => {
return console.error(err);
});
In general an unhandled Promise rejection means that you're missing a catch method to deal with the error. Simply including .then() after returning a promise only deals with the code if it runs successfully, whereas including a .catch block will skip .then and only run .catch, with the error as a callback when an error occurs while executing the code which returns the promise.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
myModel.save()
.then(() => {
console.log('Saved');
})
.catch(err => {
console.log(err);
}
const express = require('express');
const cors = require('cors');
const massive = require('massive');
const bodyParser = require('body-parser');
const config = require('../config');
const app = express();
app.use(bodyParser.json());
//massive connection string to database
massive(config.dblink).then(db => {
app.set('db', db)
app.get('db').seed_file().then(res => {
console.log(res)
})
}).catch(err => {
console.log(err)
});
const port = 3001;
app.listen(port, () => {console.log(`the server is listening on ${port}`)})
I am getting the following error:
(node:173676) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 2): error: syntax error at or near "{"
(node:173676) [DEP0018] DeprecationWarning: Unhandled promise rejections are
deprecated. In the future, promise rejections that are not handled will
terminate the Node.js process with a non-zero exit code.
I haven't been able to figure out what is wrong. I have looked at multiple different examples, but cannot see the problem. I have a .catch after my seed_file promise.
Any thoughts?
I'm getting an Unhandled Promise Rejection error but can't figure out
why
You get this warning because you have unhandled promise rejection :). The outer catch() method is not handling nested promise rejections, so two options could be:
1) Use return on your nested promise and it will be caught from the outer catch():
massive(config.dblink).then(db => {
app.set('db', db)
return app.get('db').seed_file().then(res => {
console.log(res)
});
}).catch(err => console.log(err) });
2) Use inner catch() to handle differently the nested rejection:
massive(config.dblink).then(db => {
app.set('db', db)
app.get('db').seed_file().then(res => {
console.log(res)
}).catch(err => console.log(err) });
}).catch(err => console.log(err) });
Demonstration:
function doPromise(someText, flag) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
flag ? resolve(someText) : reject(someText);
}, 500);
});
}
/* The following sample demostrates unhandled rejection */
doPromise('this will resolve', true).then(function(res1) {
console.log(res1);
doPromise('this is unhandled promise rejection', false).then(function(res2) {
console.log(res2);
});
});
/* The following sample demostrates handling nested promise rejection like explained in point 1) */
doPromise('1) this will resolve', true).then(function(res1) {
console.log(res1);
return doPromise('1) nested rejection catched from outside', false);
}).catch(err => console.log(err));
/* The following sample demostrates handling nested promise rejection like explained in point 2) */
doPromise('2) this will resolve', true).then(function(res1) {
console.log(res1);
doPromise('2) nested rejection catched from inside', false).catch(err => console.log(err));
}).catch(err => console.log(err));