0

The use case is to take some user input from the browser and then insert that data into a database.

I set up a server.js:

app.use(express.static('./app'));

app.get('/', function(req, res) {
  res.sendFile(path.resolve('./app/index.html'));
});

I also have a index.html to go with it. I am importing add_response.js into the html.

<script src="js/add_response.js"></script>

<form ng-submit="submit_add()" ng-controller="AddResponse" style="min-height: 230px; margin: 0 auto;">
    <div>
        <textarea name="response_add" ng-model="response_add" rows="10" cols="80"></textarea>
    </div>
    <div class="submitButton">
        <input type="submit" id="submit_add" value="Submit" />
    </div>
</form>

Everything runs fine to this point. Then I created add_response.js to handle grabbing some user input from the browser and inserting it into a database when the user clicks submit:

var myApp = angular.module("myApp");

myApp.controller('AddResponse', function ($scope, $http) {

    $scope.submit_add = function () {
        $scope.response = $scope.response_add;

        //insert into Couchbase DB
    }
}

The problem is that to insert into Couchbase I need to import Couchbase (which was installed with npm):

var couchbase = require('couchbase')

If I try to do this within add_response.js I get an error after starting the application and going to the browser: ReferenceError: require is not defined

From googling it seems like this is because require does not work in add_response.js because it is broswer/client side (Client on node: Uncaught ReferenceError: require is not defined).

I'm not sure the best way to deal with this issue. I'm new to learning this so very possible I've just got a poorly structured application. In the above stackoverlfow response browserify is recommended but I'm trying to use pkg to bundle this application as an executable and not sure if browserify can work with that.

Do I need pass the response_add data somewhere else to insert it into the DB? If so, how? (Trying not to overcomplicate things)

SuperCow
  • 1,523
  • 7
  • 20
  • 32
  • 3
    The Couchbase part needs to run server-side. The client-side code runs in the browser which has no access to the database. You'll need to make an HTTP request (eg using `$http`) to a route you configure on your `app` – Phil Aug 19 '19 at 23:37
  • @Phil Do you mean like $http.post("https://localhost:8090/myNewRoute", data) ? – SuperCow Aug 20 '19 at 02:31

0 Answers0