启动应用程序时对象返回初始值
当我的应用启动应用时,我的变量之一返回初始值。这是我的代码
routes.js
router.post("/build-envEP", (req, res, next) => {
consts.setNewConst("env", req.body.env);
console.log(consts.getConsts().env); // returns what I expect
res.status(201).type('application/xml').send(operations.xml);
});
constants.js
var consts = {};
module.exports.setNewConst = function(key, value){
return consts[key] = value;
}
module.exports.getConsts = function(){
return consts;
}
builder.js
...
console.log(consts.getConsts().env)
...
启动服务器时,我得到了{}
。
这里是我的控制台输出:
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
undefined
有时我得到undefined
,有时我得到{}
谢谢您的任何建议!
回答如下:您需要将contants.js
更改为:
var consts = {
setNewConst: function(key, value) {
return (consts[key] = value);
},
getConsts: function(key) {
return this[key];
}
};
module.exports = consts;
var consts = {
setNewConst: function(key, value) {
return (consts[key] = value);
},
getConsts: function(key) {
return this[key];
}
};
consts.setNewConst("a", 10);
const result = consts.getConsts("a");
console.log(result);
启动应用程序时对象返回初始值
当我的应用启动应用时,我的变量之一返回初始值。这是我的代码
routes.js
router.post("/build-envEP", (req, res, next) => {
consts.setNewConst("env", req.body.env);
console.log(consts.getConsts().env); // returns what I expect
res.status(201).type('application/xml').send(operations.xml);
});
constants.js
var consts = {};
module.exports.setNewConst = function(key, value){
return consts[key] = value;
}
module.exports.getConsts = function(){
return consts;
}
builder.js
...
console.log(consts.getConsts().env)
...
启动服务器时,我得到了{}
。
这里是我的控制台输出:
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
undefined
有时我得到undefined
,有时我得到{}
谢谢您的任何建议!
回答如下:您需要将contants.js
更改为:
var consts = {
setNewConst: function(key, value) {
return (consts[key] = value);
},
getConsts: function(key) {
return this[key];
}
};
module.exports = consts;
var consts = {
setNewConst: function(key, value) {
return (consts[key] = value);
},
getConsts: function(key) {
return this[key];
}
};
consts.setNewConst("a", 10);
const result = consts.getConsts("a");
console.log(result);