Unable to define and use heleper function value as global variable in node js

sam curb

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`]`
Erfan HosseinPoor

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.

edited at
0

Comments

0 comments
Login to comment

Related

unable to use global variable inside function in python

How to define jade as a global variable in node.js Express?

Define global variable in a JavaScript function

Define global variable inside a function

Unable to store value into global variable from anonymous function - Javascript and Protractor

Unable to assign value to global variable inside function, Python 3.8.10

Unable to define a global variable in C++

unable to use createCanvas as a global variable?

Updating value of variable in Node js promise function

How to use Node.js global variable in mocha tests

How to define empty variable in python or pass value from a function to global variable?

Is it possible to define global variables in a function and use the variable in other function using preprocessor directives in C++?

Change the value of a global variable use in the function I test

Define global variable within a function in q kdb

How to define global variable inside a function?

How to pass a variable from a Request as the return value of the global function js

In rust, how do I define a global variable/constant whose value is the result of a function?

Pass value of a function to a global variable

How to use the value of a global variable?

How to assign value to a Global Variable for global use

Unable to access the modified value of a global variable from outside the function: Variable not updated

Unable to store result value from a tesseract function to a global variable inside an asynchronous function

Unable to return value from Promise function Node.js

unable to pass table td value to JS function variable

unable to set global variable in subscribe function

Unable to access the updated global variable's value

Global variable loses its value after providing its value in a function.Unable to access its value outside that function

Unable to assign value to global variable through function (shared library injection on Linux)

Initiate a value for a exported variable using exported function Node js