全局变量未使用nodejs更新 [英] global variable is not updating using nodejs
本文介绍了全局变量未使用nodejs更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在全局变量下面,尝试用不同的方法更新
I have below global variable and trying to update in diffrent methods
var companyName;
通过这种方法,我可以获得公司名称,并能够在终端中打印companyName
In this method i am getting company name and can able to print the companyName in terminal
var once = async function(myDB) {
if(once.done) return;
companyName = await myCacheData.defaultCompany();
console.log('Company inside once :' + companyName);
};
在这种方法中,我试图获取更新的公司名称,该名称在方<$ c中更新$ c> once()函数。但是当我尝试在终端中打印时,它显示 undefined
。
In this method i am trying to get updated company name, which one is updated in side once()
function. but when i tried to print in terminal it is showing undefined
.
db.dbConnect(function (err) {
if (err) {
process.exit(1)
}
else {
myDB = db.getDBConnection();
app.myDB = myDB;
once(myDB);
console.log('Company inside db call :' + companyName);
}
});
推荐答案
尝试更改此内容:
once(myDB);
console.log('Company inside db call :' + companyName);
至:
once(myDB).then(v=>console.log('Company inside db call :' + companyName)).catch(v=>console.log('error',v))
因为 once(myDB)
是 async
并返回 promise
,您可以将回调放入 promise.then(v => / *您的回调* /)
because once(myDB)
is async
and returns a promise
you can put the callback in the promise.then(v=>/*your callback*/)
这篇关于全局变量未使用nodejs更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文