javascript - Firebase Cloud Functions: how to get data from `change` and `context`? - Stack Overflow

The Firebase SDK for Cloud Functions Migration Guide: Beta to version 1.0 documentation says that the C

The Firebase SDK for Cloud Functions Migration Guide: Beta to version 1.0 documentation says that the Cloud Function trigger onUpdate parameters are now (change, context). If I log change I get a object:

Change {
  before: 
   QueryDocumentSnapshot {
     _ref: DocumentReference { _firestore: [Object], _referencePath: [Object] },
     _fieldsProto: { word: [Object] },
     _readTime: undefined,
     _createTime: '2018-04-10T15:37:11.775234000Z',
     _updateTime: '2018-04-10T15:58:06.388975000Z' },
  after: 
   QueryDocumentSnapshot {
     _ref: DocumentReference { _firestore: [Object], _referencePath: [Object] },
     _fieldsProto: { word: [Object] },
     _readTime: undefined,
     _createTime: '2018-04-10T15:37:11.775234000Z',
     _updateTime: '2018-04-10T15:58:06.388975000Z' } }

The documentation says that I can get the data from this object with change.before.val() and change.after.val(). But logging change.before.val() results in this error message:

TypeError: change.before.val is not a function

Logging change.after.val() produces this error message:

TypeError: Cannot read property 'val' of undefined

Logging context results in this object, which doesn't include the data I want:

{ eventId: 'a981ffc3-a07a-4b17-8698-0f3ef6207ced-0',
  timestamp: '2018-04-10T17:03:00.699887Z',
  eventType: 'google.firestore.document.update',
  resource: 
   { service: 'firestore.googleapis',
     name: 'projects/languagetwo-cd94d/databases/(default)/documents/Oxford_Dictionaries/Word_Request' },
  params: { Word_Request: 'Word_Request' } }

Do the (change, context) parameters only work with Realtime Database and not with Cloud Firestore?

Here's my code:

exports.oxfordPronunciation = functions.firestore.document('Oxford_Dictionaries/{Word_Request}').onUpdate((change, context) => {

console.log(change);

  let options = {
    url: '',
    headers: {
      'app_id': 'TDK',
      'app_key': 'swordfish'
    }
  };

  function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
      var word = JSON.parse(body);
      admin.firestore().collection('Oxford_Dictionaries').doc('Word_Response').set({ 'token': word });
    }
  }

  request(options, callback);
  return 0;
});

Here are my Node modules:

npm list --depth=0 
functions@ /Users/TDK/LanguageTwo/functions
├── [email protected]
├── [email protected]
└── [email protected]

The Firebase SDK for Cloud Functions Migration Guide: Beta to version 1.0 documentation says that the Cloud Function trigger onUpdate parameters are now (change, context). If I log change I get a object:

Change {
  before: 
   QueryDocumentSnapshot {
     _ref: DocumentReference { _firestore: [Object], _referencePath: [Object] },
     _fieldsProto: { word: [Object] },
     _readTime: undefined,
     _createTime: '2018-04-10T15:37:11.775234000Z',
     _updateTime: '2018-04-10T15:58:06.388975000Z' },
  after: 
   QueryDocumentSnapshot {
     _ref: DocumentReference { _firestore: [Object], _referencePath: [Object] },
     _fieldsProto: { word: [Object] },
     _readTime: undefined,
     _createTime: '2018-04-10T15:37:11.775234000Z',
     _updateTime: '2018-04-10T15:58:06.388975000Z' } }

The documentation says that I can get the data from this object with change.before.val() and change.after.val(). But logging change.before.val() results in this error message:

TypeError: change.before.val is not a function

Logging change.after.val() produces this error message:

TypeError: Cannot read property 'val' of undefined

Logging context results in this object, which doesn't include the data I want:

{ eventId: 'a981ffc3-a07a-4b17-8698-0f3ef6207ced-0',
  timestamp: '2018-04-10T17:03:00.699887Z',
  eventType: 'google.firestore.document.update',
  resource: 
   { service: 'firestore.googleapis.',
     name: 'projects/languagetwo-cd94d/databases/(default)/documents/Oxford_Dictionaries/Word_Request' },
  params: { Word_Request: 'Word_Request' } }

Do the (change, context) parameters only work with Realtime Database and not with Cloud Firestore?

Here's my code:

exports.oxfordPronunciation = functions.firestore.document('Oxford_Dictionaries/{Word_Request}').onUpdate((change, context) => {

console.log(change);

  let options = {
    url: 'https://od-api.oxforddictionaries./api/v1/entries/en/ace/pronunciations%3B%20regions%3Dus',
    headers: {
      'app_id': 'TDK',
      'app_key': 'swordfish'
    }
  };

  function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
      var word = JSON.parse(body);
      admin.firestore().collection('Oxford_Dictionaries').doc('Word_Response').set({ 'token': word });
    }
  }

  request(options, callback);
  return 0;
});

Here are my Node modules:

npm list --depth=0 
functions@ /Users/TDK/LanguageTwo/functions
├── [email protected]
├── [email protected]
└── [email protected]
Share Improve this question edited Apr 10, 2018 at 18:01 Peter Haddad 81k26 gold badges145 silver badges148 bronze badges asked Apr 10, 2018 at 17:12 Thomas David KehoeThomas David Kehoe 11k18 gold badges75 silver badges123 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

In Firestore you need to use data() instead of val():

exports.dbWrite = functions.firestore.document('/path').onWrite((change, context) => {
const beforeData = change.before.data(); // data before the write
const afterData = change.after.data(); // data after the write
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745148186a4613742.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信