1

I have this file to add a new Ship and the script to populate de fleet dropdown menu works fine:

new.ejs file:

<% include ../partials/header %>

<div class="container">
    <div class="row">
        <h1 style="text-align: center;">Create a new Ship</h1>
        <div style="width: 30%; margin: 25px auto;">
            <form action="/ships" method="POST">
                <div class="form-group">
                    <input class="form-control" type="text" name="name" placeholder="name">
                </div>
                <div class="form-group">
                    <input class="form-control" type="number" name="tons" placeholder="tons" min="0" step="1">
                </div>
                <div class="form-group">
                    <input class="form-control" type="text" name="image" placeholder="img url">
                </div>
                <!--<div class="form-group">-->
                <!--    <input class="form-control" type="text" name="fleet" placeholder="fleet">-->
                <!--</div>-->

                <div class="form-group">
                    <select class="form-control" type="text" name="fleet" id="selectNumber" placeholder="fleet">
                        <option>Choose a fleet</option>
                    </select>
                </div>

                <script>
                    var select = document.getElementById("selectNumber");
                    var options = ["First fleet", "Second fleet", "Metropolitan fleet", "Support fleet"];
                    for(var i = 0; i < options.length; i++) {
                        var opt = options[i];
                        var el = document.createElement("option");
                        el.textContent = opt;
                        el.value = opt;
                        select.appendChild(el);
                    }
                </script>




                <div class="form-group">
                    <input class="form-control" type="text" name="description" placeholder="description">
                </div>
                <div class="form-group">
                    <button class="btn btn-lg btn-primary btn-block">Submit!</button>
                </div>
            </form>
            <a href="/ships/">Back</a>
        </div>
    </div>

</div>


<% include ../partials/footer %>

I want to separate the "logic" from the view, so I need to create a file: ../public/js/jsscripts.js

and I guess inside the jsscripts.js

module.exports = {
  PopulateFleet: function () {
var select = document.getElementById("selectNumber");
                    var options = ["First fleet", "Second fleet", "Metropolitan fleet", "Support fleet"];
                    for(var i = 0; i < options.length; i++) {
                        var opt = options[i];
                        var el = document.createElement("option");
                        el.textContent = opt;
                        el.value = opt;
                        select.appendChild(el);
                    }

};

1) Do I need to require the file "jsscripts" from my app.js ? Something like:

jscripts= require ("./public/js/jscripts");

2) How to call the function "PopulateFleet" from the new.ejs file? something like??

<% PopulateFleet %>

or

<% jscripts.PopulateFleet %>

Folder structure

Fleet - public - js -> jsscripts.js

Fleet - views - ships -> - new.ejs

5
  • 2
    use <% jscripts.PopulateFleet() %> Commented Feb 4, 2019 at 12:48
  • Thanks for the help by I get the error "jsTools is not defined" on the ejs file Using: var jsTools = require("./public/js/jscripts"); on the app.js file Commented Feb 4, 2019 at 18:57
  • "You can´t" say this link: stackoverflow.com/questions/47001537/… Commented Feb 4, 2019 at 20:34
  • 1
    Is your newejs file in public folder? Can you show me your folder structure? Commented Feb 5, 2019 at 5:07
  • Added at the bottom Commented Feb 5, 2019 at 10:06

2 Answers 2

2

I have the same issue and I find a solution. So I know this solution is not the ideal solution but it's works.

I have a function's file "my_functions.js" with code like that :

var processSomething = function(variableXXXX)
{
    ...
    ...
    return(variableNew)
}

module.exports.processSomething     = processSomething;

I require this file into "application.js" like that :

var myFonctions = require('./include/my_fonctions.js');

And I pass this variable to my EJS like that

res.render('view.ejs',{myFonctionsForEjs:myFonctions});

And, in my EJS File I can do that

var response = myFonctionsForEjs.processSomething(1234);
Sign up to request clarification or add additional context in comments.

Comments

1

use <%= jscripts.PopulateFleet() %>

8 Comments

I'm using C9 as online IDE, and the require line only works with jscripts = require ("./public/js/jsscripts"); but still I get the error "jscripts is not defined" on the new.ejs file
i am pretty sure you are not including the file correctly=> try this .. require ("../../../public/js/jsscripts");
I tryed the first time I read your answer and I get error when runining node app.js "Error: Cannot find module '../../../public/js/jsscripts' at Function.Module._resolveFilename (module.js:469:15)" But with ./public/js/jsscripts runs ok. The problem is calling javascript on the ejs template
Hmm I am not able to figure out your error. Could you send me a codesandbox or jsfiddle link? Whichever is convinient
Path looks fine. Just a silly mistake. i missed a "=". Updated my answer. Check this and use '-' or '=' according to your requirement. stackoverflow.com/questions/48522768/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.