【问题标题】:Cloud Functions v1, wildcards not working in functions.firestore.onCreate, empty context.paramsCloud Functions v1,通配符在 functions.firestore.onCreate 中不起作用,为空 context.params
【发布时间】: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 应该包含 userIdexercisetimeId 的通配符值,它是空的。 这是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});
})

【问题讨论】:

标签: javascript firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

解决方案是将params 作为附加参数传递给包装函数。

例如:

let data = test.database.makeDataSnapshot({
    valProp1: "hello",
    valProp2: 12345
}, "my/path/param1/param2");

await test.wrap(myFunctions.func1)(data,
    {
        params: {
            param1: userId,
            param2: orderId
        }
    });

正如在issue 中看到的那样。

【讨论】:

    猜你喜欢
    • 2020-07-02
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    相关资源
    最近更新 更多