Tho*_*hoe 2 javascript firebase google-cloud-functions google-cloud-firestore
在火力地堡SDK云功能迁移指南:测试版到1.0版文档中说,云计算功能的触发onUpdate参数现在(change, context)。如果我登录,change我会得到一个对象:
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' } }
Run Code Online (Sandbox Code Playgroud)
文档说我可以使用change.before.val()和从此对象获取数据change.after.val()。但记录change.before.val()导致此错误消息:
TypeError: change.before.val is not a function
Run Code Online (Sandbox Code Playgroud)
日志记录change.after.val()产生此错误消息:
TypeError: Cannot read property 'val' of undefined
Run Code Online (Sandbox Code Playgroud)
context在这个对象中记录结果,它不包括我想要的数据:
{ eventId: 'a981ffc3-a07a-4b17-8698-0f3ef6207ced-0',
timestamp: '2018-04-10T17:03:00.699887Z',
eventType: 'google.firestore.document.update',
resource:
{ service: 'firestore.googleapis.com',
name: 'projects/languagetwo-cd94d/databases/(default)/documents/Oxford_Dictionaries/Word_Request' },
params: { Word_Request: 'Word_Request' } }
Run Code Online (Sandbox Code Playgroud)
这些(change, context)参数是否仅适用于实时数据库而不适用于 Cloud Firestore?
这是我的代码:
exports.oxfordPronunciation = functions.firestore.document('Oxford_Dictionaries/{Word_Request}').onUpdate((change, context) => {
console.log(change);
let options = {
url: 'https://od-api.oxforddictionaries.com/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;
});
Run Code Online (Sandbox Code Playgroud)
这是我的节点模块:
npm list --depth=0
functions@ /Users/TDK/LanguageTwo/functions
??? firebase-admin@5.12.0
??? firebase-functions@1.0.1
??? request@2.85.0
Run Code Online (Sandbox Code Playgroud)
在Firestore 中,您需要使用data()而不是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
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3832 次 |
| 最近记录: |