Coder Social home page Coder Social logo

Remote MongoDB Example about examples HOT 11 CLOSED

serverless avatar serverless commented on August 26, 2024
Remote MongoDB Example

from examples.

Comments (11)

kuashe avatar kuashe commented on August 26, 2024 2

I would like to upload this because I couldn't got mongoose to work until i explicitly call the function

db.close()

Otherwise , lambda continue to process the function , due to Node event loop never ending.

Is this specified anywhere in the documentation ?

from examples.

kuashe avatar kuashe commented on August 26, 2024 1

@leeeomaaax Thank you so much for this !

from examples.

DavidWells avatar DavidWells commented on August 26, 2024

For sure! I'd love to see this

from examples.

leeeomaaax avatar leeeomaaax commented on August 26, 2024

@kuashe , I've just been through this. I was trying to make mongoose work with lambda and everything was working well on local, both on invoke test (serverless-mocha-plugin) and invoke local. But on the lambda environment my function was timing out even though it was getting all the way through the code.

There is another way of solving this. Use this context property to prevent the lambda from executing the handler callback.

context.callbackWaitsForEmptyEventLoop = false;

This way you can keep your connection to mongo open and speed up the warm calls to your function.

from examples.

DavidWells avatar DavidWells commented on August 26, 2024

merged #150

https://github.com/serverless/examples/tree/master/aws-node-rest-api-mongodb

from examples.

kvreem avatar kvreem commented on August 26, 2024

@kuashe I am using the following https://github.com/serverless/serverless-graphql as a boilerplate, but have integrated mongoDB instead of DynamoDB (using mongo atlas). Everything works fine offline using serverless-offline plugin. But when I sls log my lambda functions once i deploy i get the following error


2018-01-28 18:58:59.726 (-08:00)	59696d6e-04a0-11e8-82ec-8974eb6ffd47	MongoDB connection error. Please make sure MongoDb is running. { MongoError: failed to connect to server [undefined:27017] on first connect [MongoError: getaddrinfo ENOTFOUND undefined undefined:27017]
    at Pool.<anonymous> (/var/task/node_modules/mongodb-core/lib/topologies/server.js:336:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:188:7)
    at Connection.<anonymous> (/var/task/node_modules/mongodb-core/lib/connection/pool.js:280:12)
    at Connection.g (events.js:292:16)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:191:7)
    at Socket.<anonymous> (/var/task/node_modules/mongodb-core/lib/connection/connection.js:189:49)
    at Socket.g (events.js:292:16)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at connectErrorNT (net.js:1021:8)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)
  name: 'MongoError',
  message: 'failed to connect to server [undefined:27017] on first connect [MongoError: getaddrinfo ENOTFOUND undefined undefined:27017]' }

In my handler.js I have similar code


exports.graphqlHandler = function graphqlHandler(event, context, callback) {
    context.callbackWaitsForEmptyEventLoop = false;

    function callbackFilter(error, output) {

        if (!output.headers) {
          output.headers = {};
        }
        // eslint-disable-next-line no-param-reassign
        output.headers['Access-Control-Allow-Origin'] = '*';
        output.headers['Access-Control-Allow-Credentials'] = true;
        output.headers['Content-Type'] = 'application/json';

        callback(error, output);
    }

    mongoose.connect(mongoString, {useMongoClient: true});

    mongoose.connection.on("error", (err) => {
        console.log("MongoDB connection error. Please make sure MongoDb is running.", err);
        process.exit();
    });

    const handler = server.graphqlLambda({ schema: myGraphQLSchema });
    return handler(event, context, callbackFilter);
};

The context.callbackWaitsForEmptyEventLoop = false; doesn't seem to be helping in my case.

Were you getting a similar error until you used db.close()?

from examples.

kuashe avatar kuashe commented on August 26, 2024

@kvreem The error seam pretty straight forward to me . It's your database connection .

Fork the repository , replace the mongo string at the top of the file to make sure you database is reachable from a lambda function.

Hence, make sure you are waiting in your graphql handler for the database connection to be open.

from examples.

kvreem avatar kvreem commented on August 26, 2024

@kuashe I am aware the connection is the problem, but since my application logic isn't exactly in the handler.js like the repo, I am a little bit confused how to refactor so that I make my handler wait for the database connection to be open. I also have 0.0.0.0 ip whitelisted since I am just testing development cluster with mongo atlas.

    const handler = server.graphqlLambda({ schema: myGraphQLSchema });
    return handler(event, context, callbackFilter);

Does the above snippet need to be moved in db.once('open' function?

Or does each query I make with graphql need to have the connection open then close it once it fetches the data? For example my query here:

export function getBookings() {
    return new Promise((resolve, reject) => {
        BookingModel
            .find()
            .then((bookings) => {
                let bookingMap = [];
                bookings = _.orderBy(bookings, ['updatedAt'], ['desc']);
                bookings.forEach((booking, index) => {
                    bookingMap[index] = booking;
                });
                resolve(bookingMap);
            })
            .catch((err) => {
                reject(err);
            });
        }
    );
}

from examples.

kuashe avatar kuashe commented on August 26, 2024

@kvreem Each individual query to Lambda needs to have the database connection open then closed.

The following code should the trick

db.once('open' , () => {
        return handler(event, context, callbackFilter);
  })

from examples.

kvreem avatar kvreem commented on August 26, 2024

@kuashe sorry to keep bugging you, here is my refactored handler, but i still get the same result in my logs when i deploy to lambda.

exports.graphqlHandler = function graphqlHandler(event, context, callback) {

    context.callbackWaitsForEmptyEventLoop = false;

    function callbackFilter(error, output) {

        if (!output.headers) {
          output.headers = {};
        }
        // eslint-disable-next-line no-param-reassign
        output.headers['Access-Control-Allow-Origin'] = '*';
        output.headers['Access-Control-Allow-Credentials'] = true;
        output.headers['Content-Type'] = 'application/json';

        callback(error, output);
    }

    mongoose.connect(mongoString, {useMongoClient: true});

    const db = mongoose.connection;
    const handler = server.graphqlLambda({ schema: myGraphQLSchema });

    db.on("error", (err) => {
        console.log("MongoDB connection error. Please make sure MongoDb is running.", err);
        process.exit();
    });

    db.once('open', () => {
       return handler(event, context, callbackFilter);
    })
};

I am trying to understand how I can keep the Lambda function from timing out, based on the logs it never connects to the database. When I deploy I do get a CORS issue,

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://MY DOMAIN' is therefore not allowed access. The response had HTTP status code 502. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

But this shouldn't stop the database from connecting. Or is this whats causing the issue? I have the headers in my handler.

In my serverless.yml here is how I am defining my lambda functions. (Same as you are)


functions:
  graphql:
    handler: handler.graphqlHandler
    events:
    - http:
        path: graphql
        method: post
        cors: true

...

Again I appreciate all your help. Thanks a ton 👍

from examples.

DiptarkBose avatar DiptarkBose commented on August 26, 2024

I am trying to connect to mlab from aws lambda, and am not keen to use serverless. Can someone please help me out? My query is posted in detail @ stackoverflow: https://stackoverflow.com/questions/50559243/not-being-able-to-connect-to-mongodb-from-aws-lambda

from examples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.