How to connect to postgres db from angularjs controller

I want to save some user-defined values, name and email address in a Postgres database in an application hosted by Heroku. This is the code in my controller;

var pg = require('pg');
...
$scope.addUser = function() {
    pg.connect(process.env.DATABASE_URL, function(err, client) {
        var query = client.query('INSERT INTO users (name, email) VALUES (\'' + $scope.user.name + '\', \'' + $scope.user.email + '\')');

        query.on('row', function(row) {
            console.log(JSON.stringify(row));
        });
    });
};

I get an error that the requirement is not defined, which I know because it is not supported on the client side. I tried using the browser in the controller file, and then updated the tag <script>in index.html, which sends the file. This leads to another error.

Removing var pg = require('pg');gives "ReferenceError: pg undefined". I just want to connect to DB Postgres from my application and insert some values. Am I on the right track, or should I go about it differently?

+4
1

. PostgreSQL (pg) (express/ node.js) , . () (, Node.js), -- (BaaS), Firebase IrisCouch, .

+2

Source: https://habr.com/ru/post/1537588/


All Articles