[英]how to return a function inside a function javascript/nodejs?
i am trying to return the url i get inside the getsignedurl. 我试图返回我在getsignedurl中得到的URL。 when i try this url1 is undefined
当我尝试此url1是不确定的
function getPublicUrl(filename) {
var url1;
var file = bucket.file(filename);
file.getSignedUrl({
action: 'read',
expires: '03-17-2025'
}, function (err, url) {
if (err) {
console.error(err);
return;
} else {
url1 = url
console.log(url);
}
});
return url1;
}
then in another function i would call the getpublicurl 然后在另一个函数中,我将调用getpublicurl
stream.on('finish', function() {
req.file.cloudStorageObject = gcsname;
req.file.cloudStoragePublicUrl = getPublicUrl(gcsname);
file.getSignedUrl
seems to be an asynchronous function, you can use ES6 Promise in getPublicUrl
, some way like this file.getSignedUrl
似乎是一个异步函数,您可以在getPublicUrl
使用ES6 Promise ,类似这样
function getPublicUrl(filename) {
return new Promise(function(resolve, reject) {
var file = bucket.file(filename);
file.getSignedUrl({
action: 'read',
expires: '03-17-2025'
}, function(err, url) {
if (err) {
reject(err);
} else {
resolve(url);
}
});
});
}
stream.on('finish', function() {
getPublicUrl(gcsname).then(function(url) {
console.log(url);
req.file.cloudStorageObject = gcsname;
req.file.cloudStoragePublicUrl = url;
}).catch(function(err) {
console.log(err);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.