2

I Connected my mongoose to MongoDb database and this database hosted by mLab.

try {
    mongoose.connect('mLab Hosting Url', { useNewUrlParser: true })
} catch (e) {
    console.error('An Error Occurred When Trying Connect To MongoDb.', e);
}

And hosted my Express api which is require connecting to this db by firebase hosting.

router.get('/hello', (request, response) => {
        try {
            User.findOne({ email: 'm.yaman.katby@gmail.com' }).then((result) => {
                return response.status(200).json(result);
            }).catch((e) => {
                return response.status(700).json(e);
            });
        } catch (e) {
            return response.status(700).json(e);
        }
    });

Now when I call the api locally it is work, When i deploy the api to firebase and call it not work and return this error "Error: could not handle the request"

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

I found the problem in the firebase logs, You can not connect mLab hosted Database from firebase hosted Express Api. And the reason is firebase not allow External network.

  • 1
    Nice share Mayaka ! nice that you found the solution – Gastón Saillén Aug 09 '18 at 12:50
  • 1
    To connect to 3rd party web services from Cloud Functions, your project will need to be on a paid plan. This is to prevent abuse, since otherwise Cloud Functions could easily be used by unknown developers to DoS 3rd party services. To allow calls to 3rd party web services from your Cloud Functions code, upgrade your project to a paid plan. – Frank van Puffelen Aug 09 '18 at 13:43