Javascript 云功能在本地工作,但部署后不能工作

Javascript 云功能在本地工作,但部署后不能工作,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,我正在写我的第一个云函数,它用微笑代替了“快乐”这个词 const functions = require("firebase-functions"); const admin = require('firebase-admin'); admin.initializeApp(); exports.emojify = functions.database.ref("/messages/{pushId}/text").onWrite((change, context) => { c

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

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, "
const original = change.before.val();
is the data before the write. So if there was no data at the
"/messages/{pushId}/text"
node before you write there, the variable
original
will be null.

You shoud change to:

const original = change.after.val();
which is the data after the write, i.e. your new data, which you want to "emojify".


Update following your comments below

You should use the
update()
method (doc here), as follows:

return admin.database().ref("/messages/" + context.params.pushId + "/").update({ text: emojified });
常量函数=RequireRefireBase函数; const admin=需要“firebase-admin”; admin.initializeApp; exports.emojify=functions.database.ref/messages/{pushId}/text.onWritechange,context=>{ const original=change.before.val; const emojified=emojifyTextoriginal; 返回admin.database.ref.setemojified; }; 函数emojifyTexttext{ 设t=文本; t=t.replace/\bH | happy\b/ig,const original=change.before.val;是写入之前的数据。因此,如果在/messages/{pushId}/text节点写入之前没有数据,则变量original将为null

你应该改成:

const original=change.after.val;这是写入后的数据,即您要表情化的新数据

更新以下您的评论

您应该使用更新方法doc,如下所示:


执行此操作时,它会擦除所有现有消息,甚至消息路径,并将消息路径替换为我编写的输入文本。至少该函数起作用了。我尝试了这些和更多操作,但都导致出现错误消息或没有错误消息,但更新未应用。错误第一个参数必须是包含要重新设置的子项的对象放置。我要更新我的问题,让你看看现在如何。请详细阅读更新的工作原理。你应该创建一个具有键/值对的对象,并将其用作参数。@MFGABRIELE92供参考,我已经用你应该使用的更新方法更新了我的答案。我仍然有问题。我尝试了很多,但没有成功。你的说明方式ted me to do实际上会创建一个名为{pushId}的子项。然后,我学习了如何使用上下文检索pushId,但是,替换的文本是空的,我有这个错误。替换不是一个函数。我不知道该怎么做,这太难了,太烦人了。