【问题标题】:Cloud firebase function error promise is not definedCloud firebase 函数错误承诺未定义
【发布时间】:2019-07-26 00:36:22
【问题描述】:

我之前有两篇文章试图解决我的问题,并决定将所有现有代码排除在外。

我无法得到任何工作承诺。因此,我尝试复制 Doug Stevenson 的 YouTube 视频“Learn JavaScript Promises (Pt.1) with HTTP Triggers in Cloud Functions - Firecasts”中的示例。基于以下错误,我怀疑我的环境是否是我无法让 Promise 正常工作的原因。

这些消息都没有出现在控制台日志中(根据返回的消息,我并不感到惊讶。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const sendgrid = require('@sendgrid/mail');

admin.initializeApp();
admin.firestore().settings({
  timestampsInSnapshots: true
});
    exports.getMatchesNew = functions.https.onRequest((request, response) => {
      console.log("In On Call", request);
      admin.firestore().doc('city/seattle').get();

      promise.then(snapshot => {
          const data = snapshot.data();
          console.log("data: ", data);
          response.send(data);
      })
      .catch(error => {
        // Handle the error
        console.log(error);
        response.status(400).send(error);
      });
    });

我在本地测试此功能时收到此错误 使用 http://localhost:5000/wsos-base/us-central1/getMatchesNew

stack "ReferenceError: promise is not defined\n 在exports.getMatchesNew.functions.https.onRequest (C:\WSOS-BASE\nitrofreddo-master\functions\index.js:317: 3)\n 在 cloudFunction (C:\WSOS-BASE\nitrofreddo-master\functions\node_modules\firebase-functions\lib\providers\https.js:57:9)\n 在 app.use (C:\Users\ miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\@google-cloud\functions-emulator\src\supervisor\worker.js:151:11)\n 在 Layer.handle [as handle_request] (C: \Users\miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\layer.js:95:5)\n 在 trim_prefix (C:\Users\miker\AppData\Roaming\npm \node_modules\firebase-tools\node_modules\express\lib\router\index.js:317:13)\n 在 C:\Users\miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib \router\index.js:284:7\n 在 Function.process_params (C:\Users\miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\i ndex.js:335:12)\n 在下一个 (C:\Users\miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:275:10)\n在 app.use (C:\Users\miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\@google-cloud\functions-emulator\src\supervisor\worker.js:123:7)\n 在Layer.handle [as handle_request] (C:\Users\miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\layer.js:95:5)" 消息“未定义承诺” 名称“参考错误”

我没有从以前的开发者那里得到关于这方面的信息。我在设置环境时下载了最新的 Node.JS,但可能是不正确的。这是 Node.Js 版本冲突吗?还是 Firebase 版本冲突?还是???

请看我之前的帖子This shows a cloud function that is working, except for what I believe are dropped promises when trying to get user information for each mentee id

【问题讨论】:

  • 好吧,我没有看到 promise 被定义在任何地方?向我们展示它的定义位置。

标签: javascript google-cloud-functions


【解决方案1】:

错误消息告诉您从未定义变量promise

而不是这个:

admin.firestore().doc('city/seattle').get();

也许你的意思是这样说的:

const promise = admin.firestore().doc('city/seattle').get();

【讨论】:

  • 留一些给我们其他人回答道格;)
  • Doh!,作者当然会注意到操作符错误。谢谢。编辑和不同的错误。
  • @MikeRees 如果您找到了问题的解决方案,请回答您自己的问题。请不要编辑现有答案。
  • 注明。谢谢。虽然,我没有找到解决我的问题的方法。它导致了一个不同的错误。我应该单独发布附加错误吗?我真的在这里旋转我的轮子。我无法从引用的视频中获取您的示例,并且错误非常具有描述性,代码“MODULE_NOT_FOUND”(这是在浏览器中使用 firebase 服务命令输出中的 url 的结果)
  • 如果您有新错误,通常最好发布一个新问题。
【解决方案2】:

我不明白,但重新启动机器后,我的功能可以正常工作。可悲的是,但这仍然是解决某些技术问题的可行解决方案。 这是来自 Doug Stevenson 的引用示例中的函数(没有我未定义的“承诺”对象)。我觉得我现在处于一个很好的位置,并且现在我可以将这些承诺链接起来。感谢 Doug,以及其他想要回答但无法在 Doug 之前到达那里的人! ;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2017-04-19
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 2019-06-09
    相关资源
    最近更新 更多