gpt4 book ai didi

node.js - 使用 firebase 和 node.js 时出现回调问题

转载 作者:太空宇宙 更新时间:2023-11-04 00:07:26 25 4
gpt4 key购买 nike

我正在尝试获取 Cloud Storage 中图像的signedURL 并将其返回到node.js 服务器的post 方法。

但是返回的值总是未定义。请引用代码。

非常感谢任何帮助。

router.post('/', upload.single('file'), function(req, res) {
var sign;
var signedFinal = getUrl('cpu2.png',function(){
console.log("hello" +signedFinal);
});



function getUrl(image, callback){
const file = bucket.file(image);
const action = 'read';
const expires = '03-09-2491';
file.getSignedUrl({action:"read", expires}).then(function(url){
sign=url[0];
return url[0];
}).catch(function (error) {
{
console.log(err);
}
});
callback();
}

最佳答案

使用异步函数,元素/结果必须在回调中返回:

https://blog.risingstack.com/node-hero-async-programming-in-node-js/

function getUrl(image, callback){
const file = bucket.file(image);
const action = 'read';
const expires = '03-09-2491';
file.getSignedUrl({action:"read", expires}).then(function(url){
sign=url[0];
return callback(url[0]);

}).catch(function (error) {

return callback(null);

});

}

router.post('/', upload.single('file'), function(req, res) {

getUrl('cpu2.png',function(signedFinal){
console.log("hello" +signedFinal);

// do something here
return res.status(200).json({img: signedFinal});
});

});

关于node.js - 使用 firebase 和 node.js 时出现回调问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51595879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com