我想在Node js的函数内部访问该函数。以下是我的one.js:-
module.exports.user1 = ()=>{
updateNewUser:(req,res,next)=>{
console.log("User1")
}
}
module.exports.user2 = ()=>{
updateNewUser:(req,res,next)=>{
console.log("User2")
}
}
我正在two.js中调用该函数:-
var one = require('one.js');
console.log(one.user1.updateNewUser)
我想在one.js中访问updateNewUser。 任何帮助将不胜感激。
答案 0 :(得分:0)
module.exports = {
user1: {
updateNewUser: (req, res, next) => {
console.log("User1")
}
},
user2: {
updateNewUser: (req, res, next) => {
console.log("User2")
}
}
}
const one = require('./one.js')
one.user1.updateNewUser(<p1>, <p2>, <p3>)
答案 1 :(得分:0)
我建议您将one.js
更改为:
module.exports = {
user1: (req, res, next) => {
console.log("User1")
},
user2: (req, res, next) => {
console.log("User2")
}
};
在two.js
中:
let one = require('./one');
one.user1(); //Outputs: User1
one.user2(); //Outputs: User2
这是更干净的方法,但是如果您确实希望在函数中使用函数,则:
module.exports = {
user1: () => {
return (req, res, next) => { // Added return
console.log("User1")
}
},
user2: () => {
return (req, res, next) => { // Added return
console.log("User2")
}
}
};
注意::我添加了一个return
关键字,以便我们可以访问内部函数,而这是one.js
您在two.js
中用以下名称调用
let one = require('./one');
one.user1()(); //Outputs: User1
one.user2()(); //Outputs: User2