0

All I want to do is install MongoDB Community Version on our Debian VM in Google Cloud Platform and insert any element in the database. (Don't want to use the preinstalled packages/ programs for sale on Google Cloud Platform).

I have been stuck on trying just to insert anything into mongodb. I dont know if it I installed something incorrectly or if my code is off (or if Google has restriction). I don't know what the problem is. If someone is kind enough to help out, I would really really appreciate it.

It is really long. Please if you have time, please help...

I followed the MongoDB installation first from:

Install MongoDB Community Edition - Debian

NPM MongoDB

Here is what I did, from beginning to end.

1:) Created a VM Debian 8.0 Machine on Google Compute Engine (Installing MongoDB and NodeJS)

Make Google Cloud Work:

  1. Putty Key Generator

    • Generate
    • Move Mouse
    • Key Comment is Username
    • Key Passphrase is Password
    • Save Public & Private Key
  2. Google Cloud Add SSH Key

    • SSH Keys
    • Edit
    • Copy Paste Putty Key Code
  3. Google Cloud VM Instance

    • VM Instance
    • Create Instance
    • CentOS
    • SSH
    • View gcloud command
    • Run in Cloud Shell
  4. Linux Code

    • cd /
    • sudo su
    • sudo apt-get update
    • sudo apt-get install apache2
    • sudo apt install php5
    • cd /var/www/
    • chmod -R 0777 html/
    • cd /etc/
    • chmod -R 0777 apache2/
  5. PHPStorm

    • Tools > Deployment > Configuration...
    • STFP host: {IP ADDRESS}
    • Root Path: /var/www/html
    • Username: admin
    • Key Passphrase: password
    • {Remove index.html}
    • {Upload project}
  6. Allow .htaccess

    • Run command: a2enmod rewrite
    • Change file /etc/apache2/apache2.conf
    • Replace in "AllowOverride None" to "AllowOverride All"
    • service apache2 restart
  7. Install NodeJS

  8. Install Packages

    • cd /var/www/html/backend
    • {Have package.json already}
    • npm install express --save
    • npm install cors --save
    • npm install body-parser --save
    • npm install curl --save
    • npm install -g nodemon --save
  9. Run NodeJS forever

    • cd /var/www/html/backend
    • npm install -g forever
    • sudo chown root /etc/rc.local
    • sudo chmod 755 /etc/rc.local
    • chmod -R 0777 /etc/rc.local
    • chmod -R 0777 /etc/init.d/rc.local
    • Edit rc.local file Before exit 0, add:

      sudo service mongod start

      cd /var/www/html/backend

      forever start -c nodemon server.js

    • Upload file to /etc and /etc/init.d

    • forever start -c nodemon server.js
  10. Install MongoDB

    • cd /
    • sudo apt-get install git
    • sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
    • echo "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/3.2 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
    • sudo apt-get update
    • sudo apt-get install -y mongodb-org
    • sudo service mongod start
    • service mongod status
    • cd /var/www/html/backend
    • npm install mongodb --save
  11. Opened Port 8080 and 27017

============================================

2): The Relevant Code

So it seems that Mongodb is running and is connected. But when I want to insert data, the connection is disconnected on NodeJS. I dont know why. Here is the important relevant code:

On click, send data via AJAX to Node JS

function loginAdmin(){
    //Get elements
    const txtEmail = document.getElementById('txtEmail');
    const txtPassword = document.getElementById('txtPassword');
    const btnLogin = document.getElementById('btnLogin');

    //When all input in fields are made
    console.log("clicked");
            $.ajax({
                url: ipaddress.concat(':8080'),
                type: "POST",
                data: {
                    txtEmail: txtEmail.value,
                    txtPassword: txtPassword.value
                },
                success:function(data){
                    console.log(data);
                    return false;
                }
            });
    return false;
}

Insert Data to MongoDB

router.post('/', function(request, response) {
    var txtEmail = request.body.txtEmail;
    var txtPassword = request.body.txtPassword;

    mongo.connect("mongodb://"+ ipaddress +":27017/test", function(err, db) {
        db.collection('user-data').insertOne("223");
        txtEmail = "mongodb connecte22";
        response.send(txtEmail);
        response.end();
    });
});

Success that connection with MongoDB: enter image description here Error when attempting to insert into MongoDB: enter image description here

  • I tried many variations of db.collections insert but keeps disconnecting.... – the_begging_beginner Nov 11 '17 at 01:19
  • 1
    DON'T CONNECT INSIDE THE REQUEST. Your server-side connection is intended to be "shared" and "persisted" and not created per request. See [How do I manage MongoDB connections in a Node.js web application?](https://stackoverflow.com/q/10656574/2313887) to understand what you are supposed to be doing. – Neil Lunn Nov 11 '17 at 02:25

0 Answers0