node.js - 从 iOS 应用程序调用时,Firebase 函数返回空值
问题描述
我正在从我的 iOS 应用程序调用一个可调用的 firebase 函数,并且得到一个 null 的返回值。几天前才返回正确的值,但现在它总是返回 null。数据在返回行之前的控制台中正确记录,并且 iOS 调用中没有出现错误。
exports.startPlaylist = functions.https.onCall((data, context) => {
const uid = context.auth.uid;
const signature = data.signature;
return axios.post('---url----', {
data: signature
}).then(function(response) {
const val = response.data;
const ref = database.ref().push();
ref.set({
host: {
uid: uid
},
users: {
uid: uid
},
books: val
}, function(error) {
if(error) {
console.log('Not set');
} else {
const info = { id: ref.key };
console.log(info) //Correct log value appears in console
return info; //Return null, however
}
});
}).catch(function(err) {
console.log(err);
});
});
Firebase 调用
Functions.functions().httpsCallable("startPlaylist").call(["signature": signature]) { (result, error) in
guard let result = result, error == nil else { return }
print(result.data) //<-- prints "null"
}
解决方案
如前所述,您应该使用 set 返回的承诺:
ref.set({
host: {
uid: uid
},
users: {
uid: uid
},
books: val
}).then(function() {
const info = { id: ref.key };
console.log(info)
return info;
})
.catch(function(error) {
console.log('Not set');
});
推荐阅读
- jquery - 第二个列表项应该被选中
- shiny - 部署我的 APP 不断请求 ggplot2
- javascript - 调整不同主题的文字颜色
- android - Butterknife 抛出 java.lang.IllegalStateException 因为找不到视图(现在不存在)
- kubernetes - 如何以及在何处将参数路径放置到调用 URL
- youtube - 在容器或盒子内颤动 youtube 视频
- ruby-on-rails - Rails 无法部署到 Elastic Beanstalk Gem 文件
- javascript - 在 DnD 上正确使用 z-index
- php - 使用 Laravel Voyager 管理包实现 REST API
- python - 如何在 Python Flask 中为帖子实现点赞按钮功能?