【问题标题】:How can i use MongoDB with Cloud Functions for Firebase?如何将 MongoDB 与 Cloud Functions for Firebase 结合使用?
【发布时间】:2018-01-17 01:21:09
【问题描述】:

我想为 Firebase 和 MongoDB 使用 Cloud Functions。问题是我不知道如何将我的 Mongo 数据库与 Cloud Functions 连接。我的数据库部署在 matlab 上。 我制作了这个架构:

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

然后我在项目 index.js 文件中需要我的架构,并导出一个名为 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)
    })
})

但给我一个错误“错误:无法处理请求”

【问题讨论】:

  • 您能否编辑您的问题以指明您在哪里看到该错误?
  • exports.getAllPatient = functions.https.onRequest((request, response) => { patient.find({}).then((data) => { response.send(data) }) })
  • 我不是在问代码 - 你已经展示过了。您是否在 Firebase 控制台、浏览器或其他地方看到错误?
  • 只是出于好奇@DougStevenson 是不是因为他无法发出出站请求而出现错误?
  • 这可能是问题所在,但这不是我希望看到的错误消息。

标签: mongodb firebase mongoose google-cloud-functions


【解决方案1】:

我最近遇到了这种类型的错误,发现 firebase free 计划不允许函数内的出站连接。如果你需要调用外部的 http/tcp 连接,你需要在火焰或火焰计划中。请参阅下面的屏幕截图或查看部分云功能 -> 出站网络在此链接Firebase Pricing

【讨论】:

    【解决方案2】:

    尝试以类似以下链接所示的方式设计云功能:-

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

    我很久以前就尝试过使用 mongoose,它运行良好,但速度很慢,因为对于每个新请求,它都会打开 mongoose 连接并为您提供数据。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 2017-08-12
      • 1970-01-01
      • 2017-09-04
      • 2019-04-02
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2017-09-07
      相关资源
      最近更新 更多