I am trying to get my head around the move to await/async in Hapi 17.
Mainly I am trying to work out how to rewrite an abstracted route to one that will work with async/await
The following is an extract from my routes\dogs.js file
const DogController = require('../controllers/dog')
exports.plugin = {
name: 'dogs',
register: async (server, options) => {
server.route({
method: 'GET',
path: '/api/dogs',
handler: DogController.list
})
With the relevant extract from ../controllers/dog
exports.list = (req, h) => {
return Dog.find({}).exec().then((dog) => {
return { dogs: dog }
}).catch((err) => {
return { err: err }
})
}
Which currently returns a list of dogs from a MongoDB database at the moment. I'd like to rewrite the route so that the handler becomes something like
handler: async (request, h) => {
return DogController.list
}
But I'm assuming that the Controller itself is what needs to change.
There is an options property in Route that you can change the behavior of Route in request lifecycle. That means you can create an object and tie it up to your route definition.
Here is your dogs.js route plugin.
exports.plugin = {
async register(server, options) {
const DogController = require('../controllers/dogs');
server.route([
{
method: 'GET',
path: '/dogs',
options: DogController.view
},
]);
},
name: 'dogs-route'
};
And here, your dog controller.
exports.view = {
description: 'list all the good dogs',
handler: async (request, h) => {
return h.view('index', {'title': 'Home Page Title'});
try {
const dogs = await Dog.find({}).exec();
return {dogs};
}
catch(e){
// or something you want
return Boom.badRequest(e.message, e);
}
}
};
Here is a full example that you can check both routes and controllers respectively.
Related
So I am using the fcm-node package in order to send notifications from the Express api route to the app using a registration token.
The function is:
const FCM = require('fcm-node');
const serverKey = ...
const fcm = new FCM(serverKey);
function sendNotification(registrationToken, title, body, dataTitle, dataBody) {
const message = {
to: registrationToken,
notification: {
title: title,
body: body
},
data: {
title: dataTitle,
body: dataBody
}
};
fcm.send(message, (err, response) => {
if (err) console.log('Error ', err)
else console.log('response ', response)
});
};
module.exports = {
sendNotification
};
I made sure that if outside the function, the notification system is running. Now In the api endpoint:
const sendNotification = require('../sendNotification');
router.get('/test', async (req, res, next) => {
sendNotification('...', 'hi', 'bye','1', '2');
return res.send(200)
};
I keep on getting the error "sendNotification" is not a function. What is the cause of this?
Expression require('../sendNotification'); is giving you a object (because you exported a object in this file), so extract what you need out.
const { sendNotification } = require('../sendNotification');
try this:
module.exports = sendNotification
and use it like this:
const sendNotification = require('../sendNotification');
aUtil.js
module.exports = {
successTrue: function(data) {
return { data: data, success: true };
},
isLoggedin: async (req, res) {
//decoded token in req header
//if decoded token success,
res.json(this.successTrue(req.decoded));
}
}
that function call in test.js
router.get('/check', aUtil.isLoggedin, async (req, res) => { ... })
I want to use the function above in that function.
But I keep getting errors.
ReferenceError: successTrue is not defined
I tried many ways.
insert 'const aUtil = require('./aUtil')`
change to 'res.json(successTrue( ... )'
Use this:
module.exports = {
successTrue: function() {
return { foo: 'bar' }
},
isLoggedin: function() {
console.log(this.successTrue())
}
}
You're exporting an object, so this refers to itself.
Also make sure you're binding aUtils if you're using it as middleware like so:
router.get('/check', aUtil.isLoggedin.bind(aUtil), async (req, res) => { ... })
Try this :
const aUtil = {
successTrue: function() { //return json obj },
isLoggedin: function() {
res.json(aUtil.successTrue( ... ));
}
}
module.exports = aUtil;
I'm trying to access a function that I'm later going to use for changing some database values. However whenever I try to build the project I get:
Error: Route.post() requires callback functions but got a [object Promise]
My initial function is:
import fetch from '../../../../core/fetch';
import history from '../../../../core/history';
export const BOXOFFICE_CHECKING_IN = 'BOXOFFICE_CHECKING_IN';
export const BOXOFFICE_CHECKED_IN = 'BOXOFFICE_CHECKED_IN';
export const BOXOFFICE_CHECKED_IN_ERROR = 'BOXOFFICE_CHECKED_IN_ERROR';
export default function checkIn() {
return async (dispatch, getState) => {
try {
dispatch({ type: BOXOFFICE_CHECKING_IN });
const state = getState();
const {
order: {
id: orderId,
},
} = state;
const response = await fetch(
`/api/event/orders/${orderId}/checkIn`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
order: orderId,
checkedIn: true,
}),
}
);
if (response.status === 200) {
dispatch({ type: BOOKING_CHECKED_IN });
} else {
const errorResponse = await response.json();
if (errorResponse.code === 'card_error') {
dispatch({ type: BOXOFFICE_CHECKED_IN_ERROR });
}
}
} catch (err) {
throw err;
}
};
}
Which then feeds to the api:
import checkIn from '../handlers/api/orders/checkInCustomer';
...
export default (resources) => {
const router = new Router();
...
router.post('/orders/:orderId/checkIn', checkIn(resources));
Which then reaches the final function I wish to use:
export default async function checkIn(req, res) {
console.log('this is working fully');
return true;
}
Any help is appreciated.
The problem is, you want to be passing the function checkIn, but when you call it using checkIn(resources) you're actually passing the return value (A promise that resolves to true).
You should be using:
router.post('/orders/:orderId/checkIn', checkIn);
Now, I'm assuming you want to do this because you want to pass resources into the router.post function, correct? What happens to the request and response objects?
Where does resources go?
v
export default async function checkIn(req, res) {
console.log('this is working fully');
return true;
}
You have a few ways of accomplishing what you're looking for.
Create a resources file, and import it. This is the ideal solution:
const db = mysql.connect(...);
const lang = lang.init();
console.log('This file is only called once!');
export default {
db,
lang,
};
And then in your code (/routes/checkIn.js):
import { db } from '../resources';
export default async function checkIn(req, res) {
//Access db here
//db.query...
}
Wrap your code in an intermediate function:
router.post('/orders/:orderId/checkIn', (req, res) => checkIn(req, res, resources));
Bind() resources to your checkIn function:
const db = mysql.connect(...);
const lang = lang.init();
const resources = {db, lang};
router.post('/orders/:orderId/checkIn', checkIn.bind(resources));
And then in your code (/routes/checkIn.js):
export default async function checkIn(req, res) {
//Access this.db here
//this.db.query...
}
I'm a newbie in node js Development. I just learn node js in short time ago. Here I create a router file
import express from 'express';
import storyController from '../../controllers/story';
const router = express.Router();
router.post('/', (req, res) => {
const { author, title } = req.body;
console.log(author);
const story = {
author: req.body.author,
title: req.body.title,
content: req.body.content,
tags: req.body.tags
};
storyController.createStory(story, function(error, result){
console.log("halo");
if(error)
res.status(500).send({ success: false, message: error.message});
res.status(200).send({ success: true, message: "Success"});
});
});
Then, i create one more file referred as the controller here
import mongoose from 'mongoose';
const Story = mongoose.model('Story');
exports.createStory = async (story) => {
const { author, title } = story;
if(!author){
console.log("hahaAuthor");
return {
error: true,
message: 'You must write an author name!'
};
}
if(!title) {
console.log("haha");
return {
error: true,
message: 'You must write a title!'
}
}
const newStory = new Story({
author: author,
title: title,
content: story.content,
tags: story.tags,
slug: ''
});
newStory.save().then((story) => {
return { error: false, result: story};
}).catch((error) => {
return { error: error};
})
};
But, unfortunately I don't know why my function in router file doesn't call the callback function. The console.log doesn't even called yet. Please help. Otherwise, maybe you have a better way to do this. Thanks!
As createStory is an async function. Change your code like this. You are mixing async with Promise and callback
exports.createStory = async (story) => {
...
// Change the promise to await
let story = await newStory.save();
return { error: false, result: story};
};
Error should be handled in the controller with Promise catch clause.
Something like
router.post('/', (req, res) => {
storyController.createStory.then(data => {
return res.json({error: false, data: data});
}).catch(e => {
return res.json({error: true});
})
});
Note: Either use callback or async. async is the best option now adays
May be this can work:
// 1. callback style
newStory.save().then((story) => {
return cb(null, story);
}).catch((error) => {
return cb(error);
})
// 2. await
await newStory.save();
// controller
router.post('/', (req, res) => {
storyController.createStory.then(data => {
return res.json(...);
}).catch(e => {
return res.json(...);
});
If you use callback style, Error-First Callback is better.
I'm handling with an webapplication that is not mine and now I've got to send a hundred e-mails.
Unfortunately, the code is not documented and not so well written, that means i have to go testing it to discover what I am able to do and what I'm not. but I don't know how to access this function that is on the code via node. Is it actually possible to do it? Here's the code:
router.post('/aprovadosemail', miPermiso("3"), (req, res) => {
var templatesDir = path.resolve(__dirname, '..', 'templates');
var emailTemplates = require('email-templates');
// Prepare nodemailer transport object
emailTemplates(templatesDir, function(err, template) {
if (err) {
console.log(err);
} else {
var users = [];
projetoSchema.find({"aprovado":true, "categoria":"Fundamental II (6º ao 9º anos)"}, function (err, docs) {
if (err) throw err;
//console.log(docs);
docs.forEach(function(usr) {
let url = "http://www.movaci.com.b/projetos/confirma/"+usr._id+"/2456";
let url2 = "http://www.movaci.com.br/projetos/confirma/"+usr._id+"/9877";
users.push({'email': usr.email, 'projeto': usr.nomeProjeto, 'url': url, 'url2': url2});
});
for (var i = 0; i < users.length; i++) {
console.log(users[i]);
}
const transporter = nodemailer.createTransport(smtpTransport({
host: 'smtp.zoho.com',
port: 587,
auth: {
user: "generic#mail.com",
pass: "genericpassword"
},
getSocket: true
}));
var Render = function(locals) {
this.locals = locals;
this.send = function(err, html, text) {
if (err) {
console.log(err);
} else {
transporter.sendMail({
from: 'no-reply4#movaci.com.br',
to: locals.email,
subject: 'MOVACI - Projeto aprovado!',
html: html,
text: text
}, function(err, responseStatus) {
if (err) {
console.log(err);
} else {
console.log(responseStatus.message);
}
});
}
};
this.batch = function(batch) {
batch(this.locals, templatesDir, this.send);
};
};
// Load the template and send the emails
template('rateada', true, function(err, batch) {
for(var user in users) {
var render = new Render(users[user]);
render.batch(batch);
};
});
res.send('ok');
});
};
});
});
Seems like previous dev did not knew email-templates package deeply (at least have not read how it works).
So in fact it has send method, You can create an email object from email-templates and pass necessary defaults, then You call .send method of it by passing dynamical parts - it just simply merge additional params passed in send arguments, sends mail using nodemailer inside of promise which it returns.
If it's interesting for You - read source code of it: https://github.com/niftylettuce/email-templates/blob/master/index.js
I tried to simplify it modular parts using promises.
I've not debugged it, but You may check my solution and fix it as You wish.
Have 2 files (to routing from handler separately, it may have variables that may conflict and etc):
1) methods/users/aprovadosEmail.js:
const
Email = require('email-templates'),
const
emailTemplatesDir = path.resolve(__dirname + '/../../templates'),
smtpTransportConfig = {
host: 'smtp.zoho.com',
port: 587,
secure: false,
auth: {
user: "no-reply4#movaci.com.br",
pass: "some-password-here"
}
},
createEmail = (template, subject) => {
return new Email({
views: {
root: emailTemplatesDir,
},
transport: smtpTransportConfig,
template,
message: {
from: 'no-reply4#movaci.com.br',
subject
}
});
},
getApprovedUsers = () => {
return new Promise((resolve, reject) => {
const criteria = {
aprovado: true,
categoria:"Fundamental II (6º ao 9º anos)"
};
projetoSchema.find(
criteria,
(error, docs) => {
if(error) return reject(error);
const users = docs.map(doc => {
return {
email: doc.email,
projeto: doc.nomeProjeto,
url: "http://www.movaci.com.b/projetos/confirma/"+doc._id+"/2456",
url2: "http://www.movaci.com.br/projetos/confirma/"+doc._id+"/9877"
};
});
resolve(users);
});
});
},
sendMailToUser = (mail, user) => {
return mail.send({
message: {
to: user.email
},
locals: user
});
},
broadcastMailToUsers = (mail, users) => {
return Promise
.all(
users
.map(user => sendMailToUser(mail, user))
);
};
module.exports = (req, res) => {
const mail = createEmail('rateada', 'MOVACI - Projeto aprovado!'); // mail object
getApprovedUsers()
.then(users => broadcastMailToUsers(mail, users))
.then(result => {
console.log('Result of broadcast:', result);
res.send('ok');
})
.catch(error => {
res.status(500).send(error);
});
};
2) current routes file where routing part that uses module file:
router.post(
'/aprovadosemail',
miPermiso("3"),
require(__dirname+'/../methods/users/aprovadosEmail')
);