【问题标题】:Cloud function works locally, but not after deploying it云功能在本地工作,但在部署后不能
【发布时间】:2018-05-31 04:52:06
【问题描述】:

我正在编写我的第一个云函数,它用微笑代替了“快乐”这个词。

const functions = require("firebase-functions");
const admin = require('firebase-admin');

admin.initializeApp();

exports.emojify = functions.database.ref("/messages/{pushId}/text").onWrite((change, context) => {
    const original = change.before.val();
    const emojified = emojifyText(original);

    return admin.database().ref().set(emojified);
});

function emojifyText(text) {
    let t = text;
    t = t.replace(/\b(H|h)appy\b/ig, "????");

    console.log("Result:", t);

    return t;
};

我发现我可以在部署之前通过运行 firebase functions:shell 并执行以下操作进行测试:

firebase > emojify({ before: "Happy!" })
'Successfully invoked function.'
firebase > info: User function triggered, starting execution
info: Result: ????!
info: Execution took 2949 ms, user function completed successfully

它有效。但是,当使用我的 Android 应用进行测试时,我的函数的日志将显示:

TypeError: Cannot read property 'replace' of null
    at emojifyText (/user_code/index.js:15:13)
    at exports.emojify.functions.database.ref.onWrite (/user_code/index.js:8:23)
    at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
    at next (native)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
    at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
    at /var/tmp/worker/worker.js:716:24
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

我不明白。

经过一些新的尝试,我的代码如下:

const functions = require("firebase-functions");
const admin = require('firebase-admin');

admin.initializeApp();

exports.emojify = functions.database.ref("/messages/{pushId}/text").onWrite((change, context) => {
    // if (!change.before.val()) { return null; } 
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    // I tried with this and without it, but neither helped.

    const original = change.after.val();
    const emojified = original.replace(/\b(H|h)appy\b/ig, "????");

    return admin.database().ref("/messages/{pushId}/text").update(emojified);
});

我得到的最接近的方法是真正让它擦除基础中的所有内容,包括路径messages 并将其替换为书面文本,并将文本替换为表情符号。比如:

但它使用的是set()而不是update(),这并没有显示出任何修改的迹象。

【问题讨论】:

    标签: javascript firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    const original = change.before.val(); 是写入之前的数据。因此,如果在您写入之前"/messages/{pushId}/text" 节点处没有数据,则变量original 将为空。

    你应该改为:

    const original = change.after.val(); 这是写入之后的数据,即您想要“表情符号化”的新数据。


    在下面跟随你的 cmets 更新

    你应该使用update()方法(doc here),如下:

    return admin.database().ref("/messages/" + context.params.pushId + "/").update({ text: emojified });
    

    【讨论】:

    • 这样做时,它会删除所有现有消息甚至messages 路径,用我写的输入文本替换messages 路径。至少该功能确实有效。
    • 我尝试了这些以及更多,但都导致错误消息或没有错误消息但更新未应用。错误First argument must be an object containing the children to replace。我将更新我的问题,让您看看现在情况如何。
    • 请详细阅读 update() 的工作原理。您应该使用键/值对创建一个对象并将其用作参数。
    • @mfgabriel92 仅供参考,我已经用您应该使用update() 方法的方式更新了我的答案。
    • 我还是有问题。我尝试了很多都没有成功。你指示我做的方式实际上会创建一个名为“{pushId}”的孩子。然后我学会了如何使用context 来检索pushId,但是,替换的文本是空的,我有这个错误original.replace is not a function。我不知道该怎么办,这太难了,太烦人了。
    猜你喜欢
    • 2018-07-28
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    相关资源
    最近更新 更多