【问题标题】:firebase functions Property 'data' does not exist on type 'Change<DataSnapshot>'firebase 函数 'Change<DataSnapshot>' 类型上不存在属性 'data'
【发布时间】:2018-09-27 22:44:36
【问题描述】:

我正在尝试部署 firebase 功能,但出现错误

src/index.ts(132,31):错误 TS2339:“更改”类型上不存在属性“数据”。 src/index.ts(136,65): 错误 TS2339: “更改”类型上不存在属性“参数”。 src/index.ts(142,18):错误 TS2339:属性“数据”不存在 输入“更改”。

我的代码

    module.exports.onUserStatusChanged = functions.database
  .ref('/users/{uid}').onUpdate((event) => {
    // Get the data written to Realtime Database
    const eventStatus = event.data.val();

    // Then use other event data to create a reference to the
    // corresponding Firestore document.
    const userStatusFirestoreRef = firestore.doc(`users/${event.params.uid}`);

    // It is likely that the Realtime Database change that triggered
    // this event has already been overwritten by a fast change in
    // online / offline status, so we'll re-read the current data
    // and compare the timestamps.
    return event.data.ref.once("value").then((statusSnapshot) => {
      return statusSnapshot.val();
    }).then((status) => {
      console.log(status, eventStatus);
      // If the current timestamp for this data is newer than
      // the data that triggered this event, we exit this function.
      if (status.last_changed > eventStatus.last_changed) return;

      // Otherwise, we convert the last_changed field to a Date
      eventStatus.last_changed = new Date(eventStatus.last_changed);

      // ... and write it to Firestore.
      //return userStatusFirestoreRef.set(eventStatus);
      return userStatusFirestoreRef.update(eventStatus);
    });
  });

它显示红色下划线

event.data.val()
event.params.uid

【问题讨论】:

标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

改变这个:

module.exports.onUserStatusChanged = functions.database
.ref('/users/{uid}').onUpdate((event) => {
// Get the data written to Realtime Database
const eventStatus = event.data.val();

进入这个:

module.exports.onUserStatusChanged = functions.database
.ref('/users/{uid}').onUpdate((change,context) => {
// Get the data written to Realtime Database
const eventStatus = change.after.val();

也改变这个:

event.params.uid

进入这个:

context.params.uid

更多信息在这里:

https://firebase.google.com/docs/functions/beta-v1-diff

云函数已更新,触发onWrite后获取数据,需要使用change前后两个属性,本例使用change.after.val()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-08-22
  • 2020-01-11
  • 2018-04-05
  • 2017-08-13
  • 2020-08-02
  • 2020-05-01
  • 2018-10-05
  • 2022-10-16
相关资源
最近更新 更多