Mike Rees
Mike Rees

Reputation: 11

Cloud firebase function error promise is not defined

I have two previous posts trying to resolve my issues, and decided I would take all of the existing code out of the equation.

I have not been able to get any promises to work. So, I am trying to duplicate the samples in Doug Stevenson's YouTube video "Learn JavaScript Promises (Pt.1) with HTTP Triggers in Cloud Functions - Firecasts". Based on the error below, I question if my environment is the reason I have been unable to get promises to work properly.

Neither of these messages appear in the console logs (not surprising to me based on the message returned.

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);
      });
    });

and I get this error when attempting to test this function locally using http://localhost:5000/wsos-base/us-central1/getMatchesNew

stack "ReferenceError: promise is not defined\n at exports.getMatchesNew.functions.https.onRequest (C:\WSOS-BASE\nitrofreddo-master\functions\index.js:317:3)\n at cloudFunction (C:\WSOS-BASE\nitrofreddo-master\functions\node_modules\firebase-functions\lib\providers\https.js:57:9)\n at 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 at 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 at trim_prefix (C:\Users\miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:317:13)\n at C:\Users\miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:284:7\n at Function.process_params (C:\Users\miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:335:12)\n at next (C:\Users\miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:275:10)\n at 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 at Layer.handle [as handle_request] (C:\Users\miker\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\layer.js:95:5)" message "promise is not defined" name "ReferenceError"

I have NO information from previous developer on this. I downloaded the latest Node.JS when setting up the environment, but maybe that was incorrect. Is this a Node.Js version conflict? or a Firebase version conflict? Or ???

Please see my previous post 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

Upvotes: 1

Views: 526

Answers (2)

Mike Rees
Mike Rees

Reputation: 11

I don't understand it, but after rebooting my machine my function works. It's sad, but that is still a viable solution to some tech issues. This is the function from the referenced sample from Doug Stevenson (without my undefined 'promise' object). I feel I'm in a good place now, and will be able to chain these promises now that I have it working. Thanks to Doug, and everyone else who wants to answer but can't get there before Doug does! ;-)

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317828

The error message is telling you that you never defined the variable promise.

Instead of this:

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

Perhaps you meant to say this:

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

Upvotes: 1

Related Questions