5

I want to use Cloud Functions for Firebase and MongoDB. The problem is I don't know how to connect my Mongo database with Cloud Functions. My database is deployed at matlab. I made this schema:

var mongoose = require('mongoose')
var Schema = mongoose.Schema

var patientSchema = new Schema({

    name: {
        type: String,
        required : true,
    },

    disease:{
        type: String,
        required : true, 
    },

    medication_provided: {
        type: String,
        required : true,
    },

    date : {
        type : Date,
    }
})

const patient = mongoose.model('patientInfo', patientSchema)
module.exports = patient

Then I require my schema in project index.js file, and export a function called getAllPatient.

const patient = require('../Patient')
const functions = require('firebase-functions');
const mongoose = require('mongoose')

mongoose.connect('mongodb://patient:patient123@ds139869.mlab.com:39869/patient',{useMongoClient: true})
exports.getAllPatient = functions.https.onRequest((request, response) => {
    patient.find({}).then((data) => {
        response.send(data)
    })
})

but gives me an error that "Error: could not handle the request"

6
  • 1
    Could you edit your question to indicate where exactly are you seeing that error? Aug 9, 2017 at 9:09
  • exports.getAllPatient = functions.https.onRequest((request, response) => { patient.find({}).then((data) => { response.send(data) }) }) Aug 9, 2017 at 9:14
  • I'm not asking about the code - you already showed that. Are you seeing the error in the firebase console, or in the browser, or where else? Aug 9, 2017 at 9:17
  • Just out of curiosity @DougStevenson isn't the error there because he can't make outbound requests? Aug 9, 2017 at 9:24
  • 1
    That could be the problem, but that's not the error message I would expect to see. Aug 9, 2017 at 9:46

2 Answers 2

5

I was recently facing this type of error and found that firebase free plan doesn't allow the outbound connections from within the functions. If you need to call external http/tcp connections, you are required to be on the flame or blaze plan. see the screenshot attached below or see the section cloud functions -> outbound networking at this link Firebase Pricing

enter image description here

3

Try to design the cloud function in a similar way shown in a link below:-

https://github.com/firebase/functions-samples/blob/master/authorized-https-endpoint/functions/index.js

I've tried with mongoose long time back and it was working fine and but it's slow because for every new request it's going to open the mongoose connection and serve you the data.

Hope this helps!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.