Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Firebase函数中设置集合名称_Javascript_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Javascript 在Firebase函数中设置集合名称

Javascript 在Firebase函数中设置集合名称,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,刚开始使用Firebase函数并使示例正常工作,但很困惑,因为如果我将“messages”集合更改为其他名称(例如“listings”),则不会发生更新事件。我在“添加”和“makeUppercase”行的两个位置更改了“messages”一词。我得到了正确的响应,它将数据写入集合,但不会触发事件。必须是简单的,但不能谷歌它 const functions = require('firebase-functions'); const admin = require('firebase-admin

刚开始使用Firebase函数并使示例正常工作,但很困惑,因为如果我将“messages”集合更改为其他名称(例如“listings”),则不会发生更新事件。我在“添加”和“makeUppercase”行的两个位置更改了“messages”一词。我得到了正确的响应,它将数据写入集合,但不会触发事件。必须是简单的,但不能谷歌它

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

admin.initializeApp();

exports.addMessage = functions.https.onRequest(async (req, res) => {
    // Grab the location parameter.
    const inputcode = req.query.code || 'blank';

    // Push the new message into Cloud Firestore using the Firebase Admin SDK.
    const writeResult = await admin.firestore().collection('messages').add({inputcode: inputcode});
    // Send back a message that we've succesfully written the message
    res.json({result: `Message with ID: ${writeResult.id} added.`});
  });

exports.makeUppercase = functions.firestore.document('/messages/{documentId}')
    .onCreate((snap, context) => {
      // Grab the current value of what was written to Cloud Firestore.
      const inputcode = snap.data().inputcode;

      // Access the parameter `{documentId}` with `context.params`
      functions.logger.log('Uppercasing', context.params.documentId, inputcode);
      
      const areacode = inputcode.toUpperCase();
      const written = new Date();
      
      return snap.ref.set({written, areacode}, {merge: true});
    });
顺便说一句,我正在使用本地firebase模拟器来做这个测试

这是新版本,仅在两处将“消息”更改为“VVV”


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

admin.initializeApp();

exports.addMessage = functions.https.onRequest(async (req, res) => {
    // Grab the location parameter.
    const inputcode = req.query.code || 'blank';

    // Push the new message into Cloud Firestore using the Firebase Admin SDK.
    const writeResult = await admin.firestore().collection('vvvvv').add({inputcode: inputcode});
    // Send back a message that we've succesfully written the message
    res.json({result: `Message with ID: ${writeResult.id} added.`});
  });

exports.makeUppercase = functions.firestore.document('/vvvvv/{documentId}')
    .onCreate((snap, context) => {
      // Grab the current value of what was written to Cloud Firestore.
      const inputcode = snap.data().inputcode;

      // Access the parameter `{documentId}` with `context.params`
      functions.logger.log('Uppercasing', context.params.documentId, inputcode);
      
      const areacode = inputcode.toUpperCase();
      const written = new Date();
      
      return snap.ref.set({written, areacode}, {merge: true});
    });

嗯。道格,你的建议一个小时左右就被采纳了!我重新开始了一切,我想我明白了。如果我在这两个位置更改名称,而不重新启动,则collection.add函数将发生,并且我可以在新集合中看到记录,但onCreate事件没有触发。我不得不重新启动整个服务来重新启动部分。我感到困惑,因为一部分在工作,而另一部分不工作。谢谢您的耐心。

您是说Firestore函数根本不会触发,而函数日志中没有关于这一点的内容吗?您确定已部署该函数吗?如果使用如图所示的代码,则会触发该函数。例如,如果我将名称“messages”更改为“views”,则没有事件触发器。除了addMessage的Begin和FInshed之外,日志中没有任何内容。为什么希望该函数为不同集合中的文档运行?它明确地设置为识别特定的路径模式。我建议对问题进行编辑,以便更清楚地了解具体情况,因为这些情况与您预期的情况不符。问题中应该有足够的信息,以便任何人都可以复制问题。不,我只是尝试设置不同的集合名称。我希望函数能够运行,因为我正在执行一行“firestore().collection('vvvv')。添加“``然后侦听``functions.firestore.document('/vvvv/{documentId}')上的事件。onCreate``我所更改的是'messages'=>vvvv'。上面的代码可以工作,但我想更改名称“messages”。不知道如何更好地解释这个问题。好吧,我建议编辑这个问题,以显示新修改的代码,它的工作方式与您期望的不同。确保在测试之前部署它。同样,我们应该能够将代码从您的问题中去掉,并亲自看看它是如何工作的。