简体   繁体   中英

How to Import function in node.js?

I want have 2 functions in report.js file I want to one is report_data which is api controller and second is test I want to use test function in report_data function.
Below is my code of both functions.

var sequelize = require('../config/sequelize');
const Op = sequelize.Sequelize.Op;
var errors = require('../config/errors');
var error = errors.errors;
const helpers = require('../helpers/validations');
const logger = require('../helpers/logger').logger;
const fs = require('fs');

module.exports = {
    report_data: async (req, res) => {
        if (!req.body.id) {
            logger.warn(error.MANDATORY_FIELDS);
            return res.status(500).send(error.MANDATORY_FIELDS)
        }

        sequelize.sequelize.transaction(async (t1) => {
            console.log('socket connected')
        test(io)
           
            let result = error.OK
            logger.info(result);
            return res.status(200).send(result)

        }).catch(function (err) 
            logger.warn(err);
            console.log(err)
            return res.status(500).send(error.SERVER_ERROR)
        })
    },

    test: function (io) {
        console.log(io.sockets)
    }
};

The easiest would be to declare test as a named global function:

function test(io) {
    console.log(io.sockets)
}

module.exports = {
    report_data: async (req, res) => {
        // now you call call `test()` here
    },
    test: test,
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM