Javascript Node.js,Mongodb:如何使用`promise.then`设置全局变量?
代码中有一个Javascript Node.js,Mongodb:如何使用`promise.then`设置全局变量?,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,代码中有一个承诺。然后尝试用于设置全局变量originalUrl,但是没有更改originalUrl。任何帮助都将不胜感激 myApp.js: // create a mongoose model var urlSchema = new mongoose.Schema({ original_url: String, short_url: String }); urlSchema.plugin(findOrCreate) var Url = mongoose.model('Url', ur
承诺。然后
尝试用于设置全局变量originalUrl
,但是没有更改originalUrl
。任何帮助都将不胜感激
myApp.js:
// create a mongoose model
var urlSchema = new mongoose.Schema({
original_url: String,
short_url: String
});
urlSchema.plugin(findOrCreate)
var Url = mongoose.model('Url', urlSchema);
let promise = new Promise(function(resolve, reject) {
app.post("/api/shorturl/new", (req, res) => {
// receive an url in the post and return a
// short id. After that, the short id can be used
// to go to the original url.
// Handle the data in the POST request
// get the hostname from the request
originalUrl = req.body.url;
// Create an identifier for the url
shortUrl = md5(originalUrl).toString().slice(-7)
// test to see if the host is live
var url = new URL(originalUrl);
hostname = url.hostname;
// if host is live
dns.lookup(hostname, function (err) {
if (err == undefined){
// if not found, save the original_url and the
// short_url, return
Url.findOrCreate({ original_url: originalUrl, short_url: shortUrl}, (err, result) => {
console.log(result)
res.json({
original_url: originalUrl,
short_url: shortUrl
});
})
}
if (err != undefined){
console.log(">>> error <<<")
res.json({"error":"host down"});
}
});
resolve(originalUrl)
});
})
promise.then(
function(result) { originalUrl = result },
function(error) { /* handle an error */ }
);
console.log(originalUrl) // undefined
//创建猫鼬模型
var urlSchema=newmongoose.Schema({
原始url:String,
短url:String
});
plugin(findOrCreate)
var Url=mongoose.model('Url',urlSchema);
让承诺=新承诺(功能(解决、拒绝){
app.post(“/api/shorturl/new)”,(请求、回复)=>{
//在帖子中接收url并返回
//短id。之后,可以使用短id
//转到原始url。
//处理POST请求中的数据
//从请求中获取主机名
originalUrl=req.body.url;
//创建url的标识符
shortUrl=md5(originalUrl).toString().slice(-7)
//测试主机是否处于活动状态
var url=新的url(originalUrl);
hostname=url.hostname;
//如果主机是活的
dns.lookup(主机名、函数(错误){
如果(错误==未定义){
//如果未找到,请保存原始的\u url和
//短url,返回
findOrCreate({origin\u Url:originalUrl,short\u Url:shortUrl},(err,result)=>{
console.log(结果)
res.json({
原始url:originalUrl,
短url:shortUrl
});
})
}
如果(错误!=未定义){
console.log(“>>>error传递给then()
的函数中的代码在承诺解决之前不会运行。因此,请改为:
promise.then(
function(result) { originalUrl = result },
function(error) { /* handle an error */ }
);
console.log(originalUrl)
…您需要做更多类似的事情:
promise.then(
function(result) { originalUrl = result },
function(error) { /* handle an error */ }
).then(
function() { console.log(originalUrl); }
);
下面是一个简单的可运行示例,希望能提供一个如何工作的想法:
var originalUrl=未定义;
var承诺=新承诺((解决、拒绝)=>{
setTimeout(函数(){
解决(”http://example.com");
}, 250)
});
我保证,那么(
函数(结果){originalUrl=result},
函数(错误){/*处理错误*/}
).那么(
函数(){console.log('promise resolved:',originalUrl);}
);
log('promise pending:',originalUrl);