I am trying to showing notification in my project so thats why i am creating helperFunction and call in main server.js file and define as global variable but when it return value [object promise],If i call helper function in my other controller it return value but as global variable it return object promise
************ helper function *************
const fs = require('fs');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const models = require(appRoot + '/models');
const helpersService = () => {
const getNotification = async () => {
var findStatus = 0;
if (findStatus != '' && findStatus != null) {
var whereOptions = {
is_read: {
[Op.iLike]: '%' + findStatus + '%'
}
}
} else {
var whereOptions = {}
}
let getNotifications = await models.Notifications.findAll({
include: [{
attributes: ['id', 'full_name', 'email'],
model: models.Users,
as: 'user'
}],
where: whereOptions,
raw: true,
});
return getNotifications;
};
return {
getTranslatedText,
notificationStatus,
getNotification,
};
};
module.exports = helpersService;
**************************************
*************** server.js *************
global.appRoot = path.resolve(__dirname);
const helpersService = require(appRoot + '/services/helpers.service');
global.notifcations = helpersService().getNotification();
****************************************
**************view.ejs*******************
<li style="padding-top: 0px; border-top: none;">
<a class="dropdown-item" href="/admin/users/logout">
<%= notifcations %></a></li>
***************************************
*********** output is *****************
[object promise`]`
At fitst you must add await to your function
global.notifcations = await helpersService().getNotification();
And then your notifications is an array of object and you can't see this directly, use this method
for Example notifications[0].name
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments