I am creating and Node project using with MongoDB. I have two files such as change_language.js
and admin_event.js
. I have created a function in change_language.js
file and I called that function from other file (admin_event.js
).
change_language.js
const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
var assert = require('assert');
const request = require("request");
const Entity_option = require("../models/entity_option");
var ObjectId = require('mongodb').ObjectID;
require("../routes/admin_event");
function change_language(language_id)
{
let myRes=[];
Entity_option.aggregate([
{$match:{"language_id":ObjectId(language_id)}}
], function (err, result)
{
if (err)
{
myRes=err;
}
else
{
myRes= result;
}
console.log(myRes); // Print 1
})
return myRes;
}
module.exports.change_language = change_language;
admin_event.js
const express = require("express");
const router = express.Router();
const Event = require("../models/event");
const myLan=require("../setting/change_language").change_language;
var ObjectId = require('mongodb').ObjectID;
let language_id="5b9f5b324ae85e12a869187c";
let x = myLan(language_id);
console.log(x); // Print 2
module.exports = router;
The function works as expected. console.log(myRes)
(Print 1 line) shows the output in command prompt. But when I call that function from another file (console.log(x)
) shows undefined
. Why?