首页 > 解决方案 > 调用 firebase 函数返回 null

问题描述

我正在调用 Functions 函数来获取用户信息,但它返回 null。请告诉我解决方案。

工作区/函数/src/index.ts

exports.getUser = functions.https.onCall((data, context) => {
  admin.database().ref(`user/${data.userId}`)
    .on('value', (snapshot) => {
      if (snapshot) return snapshot.val();
    });
});

工作区/src/app/sample.component.ts

firebase.functions().httpsCallable('getUser')({
  userId: profile.userId
}).then(data => {
  console.log(data);
}).catch(error => {
  alert(error);
});

标签: javascriptangularfirebasefirebase-realtime-databasegoogle-cloud-functions

解决方案


您没有在 https 可调用中返回任何内容,请添加返回:

exports.getUser = functions.https.onCall((data, context) => {
  return admin.database().ref(`user/${data.userId}`)
    .on('value', (snapshot) => {
      if (snapshot) return snapshot.val();
    });
});

推荐阅读