我正在从我的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返回的promise:
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');
});