【发布时间】:2018-04-07 09:33:34
【问题描述】:
我将 Firebase Cloud Functions 更新到 v1.0.1,并决定尝试使用新的单元测试功能。
有一个问题我找不到解决方案:
exports.updateBestTime = functions.firestore
.document("users/{userId}/exercises/{exercise}/times/{timeId}")
.onCreate((snap, context) => {
console.log(context)
...
当这个函数被调用时,snap 是正确的,snap.ref 指向我传递的文档。 context.params 应该包含 userId、exercise 和 timeId 的通配符值,它是空的。
这是console.log(context) 的输出:
{ service: 'firestore.googleapis.com',
name: 'projects/focos-debug/databases/(default)/documents/users/userId5/exercises/exercise4/times/timeId6' },
eventType: 'providers/cloud.firestore/eventTypes/document.create',
timestamp: '2018-04-07T09:17:42.500Z',
params: {} }
我对 JavaScript 没有太多经验,所以这可能是我的错。你能帮帮我吗?
这是我用于测试的代码:
const handstandTimePath = 'users/OLlJ3NFcgYXqS8T5vetYVKjzFbo1/exercises/handstand/times/test';
const snap = test.firestore.makeDocumentSnapshot(
{time: 30},
handstandTimePath
);
// Call the wrapped function with the constructed snapshot.
const wrapped = test.wrap(myFunctions.updateBestTime);
return wrapped(snap).then(function() {
return firestore.doc(handstandTimePath).get();
}).then(function(createdSnap) {
assert.property(createdSnap.data(), 'dateAndTime');
assert.property(createdSnap.data(), 'time');
assert.include(createdSnap.data(), {time: 30});
})
【问题讨论】:
-
通配符使用似乎正确,应该在 onCreate 触发时返回路径值。我认为这个版本的错误,并会很快得到纠正。或拉取 Firebase cloude 函数的请求github.com/firebase/firebase-functions/pulls 此处有关通配符使用链接的完整信息以获取更多详细信息:firebase.google.com/docs/functions/…
标签: javascript firebase google-cloud-firestore google-cloud-functions