Can´t bind mongodb service on Appfog

175 Views Asked by At

Hi i´m trying to bind mongodb service on my expressjs app with Appfog.

I have a config file like this:

config.js

var config = {}

config.dev = {};
config.prod = {};

//DEV
config.dev.host = "localhost";
config.dev.port = 3000;
config.dev.mdbhost = "localhost";
config.dev.mdbport = 27017;
config.dev.db   = "detysi";

//PROD
config.prod.service_type = "mongo-1.8";
config.prod.json         = process.env.VCAP_SERVICES ? JSON.parse(process.env.VCAP_SERVICES) : '';
config.prod.credentials  = process.env.VCAP_SERVICES ? config.prod.json[config.prod.service_type][0]["credentials"] : null;
config.prod.mdbhost      = config.prod.credentials["host"];
config.prod.mdbport      = config.prod.credentials["port"];
config.prod.db           = config.prod.credentials["db"];
config.prod.port         = process.env.VCAP_APP_PORT || process.env.PORT;  

module.exports = config;

And this is my mongodb conf depending of the environment

app.js

if ( process.env.VCAP_SERVICES ) {
    server = new Server(config.prod.mdbhost, config.prod.mdbport, { auto_reconnect: true });
    db = new Db(config.prod.db, server);
} else {
    server = new Server(config.dev.mdbhost, config.dev.mdbport, { auto_reconnect: true });
    db = new Db(config.dev.db, server); 
}

I bind the service manually from https://console.appfog.com, my app is using the infra AWS Virginia. I also use MongoHQ addon to create one collection with two documents.

When i go to Windows console and write af update myapp it throws me next error:

Cannot read property '0' of undefined

This is cause process.env.VCAP_SERVICES is undefined.

I was investigating that and it can be that my mongodb service is incorrectly binded.

After that i tried to bind mongodb service from the windows console like below:

af bind-service mongodb myapp 

But it throws me next error:

Service mongodb and App myapp are not on the same infra

At this point i don´t know what can i do.

1

There are 1 best solutions below

0
On

I had the same problem.

What fixed it for me:

Go to: console>services>(re)start mongodb service

Run the same command again.

Push again.

It should work.