Accessing a function declared in App.js in an EJS file Using Nodejs and MongodbIn Node.js, how do I “include” functions from my other files?Download a file from NodeJS Server using ExpressGlobal Variable in app.js accessible in routes?Accessing NodeJS data in EJS fileCannot install NodeJs: /usr/bin/env: node: No such file or directoryExpress.js and Node.js : Unable to set value in the request objectExpress req.body not Workingnodejs Mongodb error whie running app.js fileJavaScript and CSS files not linking in App (using Express)REST API with Postgresql - how to make simplest frontend interface

Is there a word for the censored part of a video?

Is Diceware more secure than a long passphrase?

Restricting the options of a lookup field, based on the value of another lookup field?

How long after the last departure shall the airport stay open for an emergency return?

Why do games have consumables?

Is there a better way to say "see someone's dreams"?

How do I reattach a shelf to the wall when it ripped out of the wall?

Find the identical rows in a matrix

Which big number is bigger?

What makes accurate emulation of old systems a difficult task?

Injection into a proper class and choice without regularity

A Paper Record is What I Hamper

Why did C use the -> operator instead of reusing the . operator?

Why did Rep. Omar conclude her criticism of US troops with the phrase "NotTodaySatan"?

How exactly does Hawking radiation decrease the mass of black holes?

My bank got bought out, am I now going to have to start filing tax returns in a different state?

Older movie/show about humans on derelict alien warship which refuels by passing through a star

"My boss was furious with me and I have been fired" vs. "My boss was furious with me and I was fired"

Can someone publish a story that happened to you?

Find a stone which is not the lightest one

What is the best way to deal with NPC-NPC combat?

What is this word supposed to be?

All ASCII characters with a given bit count

What is purpose of DB Browser(dbbrowser.aspx) under admin tool?



Accessing a function declared in App.js in an EJS file Using Nodejs and Mongodb


In Node.js, how do I “include” functions from my other files?Download a file from NodeJS Server using ExpressGlobal Variable in app.js accessible in routes?Accessing NodeJS data in EJS fileCannot install NodeJs: /usr/bin/env: node: No such file or directoryExpress.js and Node.js : Unable to set value in the request objectExpress req.body not Workingnodejs Mongodb error whie running app.js fileJavaScript and CSS files not linking in App (using Express)REST API with Postgresql - how to make simplest frontend interface






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have an app.js file where I've declared a function like so:



function acceptReq(req, res) 
db.getDB().collection(collection).findOneAndUpdate(
_id: ObjectId("5c94f444dab6e423a488d2ae")
,
$set:
requested: false,
requests:
user: undefined,
time: undefined
,
Booked: true

,
returnOriginal: false
, (err, result) = >
if (err)
console.log(err);
else
req.flash(
'success_msg',
'Request Accepted!'
);
res.redirect('/dashboard/requests')

);



I also have an ejs file in which I have a button that when clicked would execute this function in app.js:



<button onclick="acceptReq()" class="btn btn-success btn-block">Accept</button>


However, whenever I click the button, the file is not able to identify acceptReq().



How do I properly declare the function for ejs to read?










share|improve this question






























    1















    I have an app.js file where I've declared a function like so:



    function acceptReq(req, res) 
    db.getDB().collection(collection).findOneAndUpdate(
    _id: ObjectId("5c94f444dab6e423a488d2ae")
    ,
    $set:
    requested: false,
    requests:
    user: undefined,
    time: undefined
    ,
    Booked: true

    ,
    returnOriginal: false
    , (err, result) = >
    if (err)
    console.log(err);
    else
    req.flash(
    'success_msg',
    'Request Accepted!'
    );
    res.redirect('/dashboard/requests')

    );



    I also have an ejs file in which I have a button that when clicked would execute this function in app.js:



    <button onclick="acceptReq()" class="btn btn-success btn-block">Accept</button>


    However, whenever I click the button, the file is not able to identify acceptReq().



    How do I properly declare the function for ejs to read?










    share|improve this question


























      1












      1








      1








      I have an app.js file where I've declared a function like so:



      function acceptReq(req, res) 
      db.getDB().collection(collection).findOneAndUpdate(
      _id: ObjectId("5c94f444dab6e423a488d2ae")
      ,
      $set:
      requested: false,
      requests:
      user: undefined,
      time: undefined
      ,
      Booked: true

      ,
      returnOriginal: false
      , (err, result) = >
      if (err)
      console.log(err);
      else
      req.flash(
      'success_msg',
      'Request Accepted!'
      );
      res.redirect('/dashboard/requests')

      );



      I also have an ejs file in which I have a button that when clicked would execute this function in app.js:



      <button onclick="acceptReq()" class="btn btn-success btn-block">Accept</button>


      However, whenever I click the button, the file is not able to identify acceptReq().



      How do I properly declare the function for ejs to read?










      share|improve this question
















      I have an app.js file where I've declared a function like so:



      function acceptReq(req, res) 
      db.getDB().collection(collection).findOneAndUpdate(
      _id: ObjectId("5c94f444dab6e423a488d2ae")
      ,
      $set:
      requested: false,
      requests:
      user: undefined,
      time: undefined
      ,
      Booked: true

      ,
      returnOriginal: false
      , (err, result) = >
      if (err)
      console.log(err);
      else
      req.flash(
      'success_msg',
      'Request Accepted!'
      );
      res.redirect('/dashboard/requests')

      );



      I also have an ejs file in which I have a button that when clicked would execute this function in app.js:



      <button onclick="acceptReq()" class="btn btn-success btn-block">Accept</button>


      However, whenever I click the button, the file is not able to identify acceptReq().



      How do I properly declare the function for ejs to read?







      node.js ejs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 19:36









      Milo

      1,96661629




      1,96661629










      asked Mar 22 at 16:45









      Hussein MenshawiHussein Menshawi

      205




      205






















          1 Answer
          1






          active

          oldest

          votes


















          0














          AFAIK you won't be able to. If you see the code acceptReq is most likely a API endpoint handler method, so you can probably define an API endpoint and use this as the handler and call that endpoint from by defining a function in javascript part of the ejs file and passing that function name in onclick of the button.



          app.get('/update', function acceptReq(req, res) 
          db.getDB().collection(collection).findOneAndUpdate(
          _id: ObjectId("5c94f444dab6e423a488d2ae")
          ,
          $set:
          requested: false,
          requests:
          user: undefined,
          time: undefined
          ,
          Booked: true


          ,
          returnOriginal: false
          , (err, result) =>
          if (err)
          console.log(err);
          else
          req.flash(
          'success_msg',
          'Request Accepted!'
          );
          res.redirect('/dashboard/requests')

          );
          );





          share|improve this answer























          • so do you mean i declare the function twice? once in app.js and the other in the ejs?

            – Hussein Menshawi
            Mar 26 at 7:51











          • @HusseinMenshawi Nope you need to define it only once, in app.js and in the ejs you can call this API using fetch function -- developers.google.com/web/updates/2015/03/introduction-to-fetch

            – Kiran Mathew Mohan
            Mar 26 at 8:16












          • I tried using the fetch function in my ejs file and i think its going through the API in app.js but, my record is not updating in the database like it is suppose to.

            – Hussein Menshawi
            Mar 27 at 10:57











          • @HusseinMenshawi Are you getting any errors in the console?

            – Kiran Mathew Mohan
            Mar 27 at 11:01











          • i get a PUT 404 error,

            – Hussein Menshawi
            Mar 27 at 11:12











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304270%2faccessing-a-function-declared-in-app-js-in-an-ejs-file-using-nodejs-and-mongodb%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          AFAIK you won't be able to. If you see the code acceptReq is most likely a API endpoint handler method, so you can probably define an API endpoint and use this as the handler and call that endpoint from by defining a function in javascript part of the ejs file and passing that function name in onclick of the button.



          app.get('/update', function acceptReq(req, res) 
          db.getDB().collection(collection).findOneAndUpdate(
          _id: ObjectId("5c94f444dab6e423a488d2ae")
          ,
          $set:
          requested: false,
          requests:
          user: undefined,
          time: undefined
          ,
          Booked: true


          ,
          returnOriginal: false
          , (err, result) =>
          if (err)
          console.log(err);
          else
          req.flash(
          'success_msg',
          'Request Accepted!'
          );
          res.redirect('/dashboard/requests')

          );
          );





          share|improve this answer























          • so do you mean i declare the function twice? once in app.js and the other in the ejs?

            – Hussein Menshawi
            Mar 26 at 7:51











          • @HusseinMenshawi Nope you need to define it only once, in app.js and in the ejs you can call this API using fetch function -- developers.google.com/web/updates/2015/03/introduction-to-fetch

            – Kiran Mathew Mohan
            Mar 26 at 8:16












          • I tried using the fetch function in my ejs file and i think its going through the API in app.js but, my record is not updating in the database like it is suppose to.

            – Hussein Menshawi
            Mar 27 at 10:57











          • @HusseinMenshawi Are you getting any errors in the console?

            – Kiran Mathew Mohan
            Mar 27 at 11:01











          • i get a PUT 404 error,

            – Hussein Menshawi
            Mar 27 at 11:12















          0














          AFAIK you won't be able to. If you see the code acceptReq is most likely a API endpoint handler method, so you can probably define an API endpoint and use this as the handler and call that endpoint from by defining a function in javascript part of the ejs file and passing that function name in onclick of the button.



          app.get('/update', function acceptReq(req, res) 
          db.getDB().collection(collection).findOneAndUpdate(
          _id: ObjectId("5c94f444dab6e423a488d2ae")
          ,
          $set:
          requested: false,
          requests:
          user: undefined,
          time: undefined
          ,
          Booked: true


          ,
          returnOriginal: false
          , (err, result) =>
          if (err)
          console.log(err);
          else
          req.flash(
          'success_msg',
          'Request Accepted!'
          );
          res.redirect('/dashboard/requests')

          );
          );





          share|improve this answer























          • so do you mean i declare the function twice? once in app.js and the other in the ejs?

            – Hussein Menshawi
            Mar 26 at 7:51











          • @HusseinMenshawi Nope you need to define it only once, in app.js and in the ejs you can call this API using fetch function -- developers.google.com/web/updates/2015/03/introduction-to-fetch

            – Kiran Mathew Mohan
            Mar 26 at 8:16












          • I tried using the fetch function in my ejs file and i think its going through the API in app.js but, my record is not updating in the database like it is suppose to.

            – Hussein Menshawi
            Mar 27 at 10:57











          • @HusseinMenshawi Are you getting any errors in the console?

            – Kiran Mathew Mohan
            Mar 27 at 11:01











          • i get a PUT 404 error,

            – Hussein Menshawi
            Mar 27 at 11:12













          0












          0








          0







          AFAIK you won't be able to. If you see the code acceptReq is most likely a API endpoint handler method, so you can probably define an API endpoint and use this as the handler and call that endpoint from by defining a function in javascript part of the ejs file and passing that function name in onclick of the button.



          app.get('/update', function acceptReq(req, res) 
          db.getDB().collection(collection).findOneAndUpdate(
          _id: ObjectId("5c94f444dab6e423a488d2ae")
          ,
          $set:
          requested: false,
          requests:
          user: undefined,
          time: undefined
          ,
          Booked: true


          ,
          returnOriginal: false
          , (err, result) =>
          if (err)
          console.log(err);
          else
          req.flash(
          'success_msg',
          'Request Accepted!'
          );
          res.redirect('/dashboard/requests')

          );
          );





          share|improve this answer













          AFAIK you won't be able to. If you see the code acceptReq is most likely a API endpoint handler method, so you can probably define an API endpoint and use this as the handler and call that endpoint from by defining a function in javascript part of the ejs file and passing that function name in onclick of the button.



          app.get('/update', function acceptReq(req, res) 
          db.getDB().collection(collection).findOneAndUpdate(
          _id: ObjectId("5c94f444dab6e423a488d2ae")
          ,
          $set:
          requested: false,
          requests:
          user: undefined,
          time: undefined
          ,
          Booked: true


          ,
          returnOriginal: false
          , (err, result) =>
          if (err)
          console.log(err);
          else
          req.flash(
          'success_msg',
          'Request Accepted!'
          );
          res.redirect('/dashboard/requests')

          );
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 17:33









          Kiran Mathew MohanKiran Mathew Mohan

          640415




          640415












          • so do you mean i declare the function twice? once in app.js and the other in the ejs?

            – Hussein Menshawi
            Mar 26 at 7:51











          • @HusseinMenshawi Nope you need to define it only once, in app.js and in the ejs you can call this API using fetch function -- developers.google.com/web/updates/2015/03/introduction-to-fetch

            – Kiran Mathew Mohan
            Mar 26 at 8:16












          • I tried using the fetch function in my ejs file and i think its going through the API in app.js but, my record is not updating in the database like it is suppose to.

            – Hussein Menshawi
            Mar 27 at 10:57











          • @HusseinMenshawi Are you getting any errors in the console?

            – Kiran Mathew Mohan
            Mar 27 at 11:01











          • i get a PUT 404 error,

            – Hussein Menshawi
            Mar 27 at 11:12

















          • so do you mean i declare the function twice? once in app.js and the other in the ejs?

            – Hussein Menshawi
            Mar 26 at 7:51











          • @HusseinMenshawi Nope you need to define it only once, in app.js and in the ejs you can call this API using fetch function -- developers.google.com/web/updates/2015/03/introduction-to-fetch

            – Kiran Mathew Mohan
            Mar 26 at 8:16












          • I tried using the fetch function in my ejs file and i think its going through the API in app.js but, my record is not updating in the database like it is suppose to.

            – Hussein Menshawi
            Mar 27 at 10:57











          • @HusseinMenshawi Are you getting any errors in the console?

            – Kiran Mathew Mohan
            Mar 27 at 11:01











          • i get a PUT 404 error,

            – Hussein Menshawi
            Mar 27 at 11:12
















          so do you mean i declare the function twice? once in app.js and the other in the ejs?

          – Hussein Menshawi
          Mar 26 at 7:51





          so do you mean i declare the function twice? once in app.js and the other in the ejs?

          – Hussein Menshawi
          Mar 26 at 7:51













          @HusseinMenshawi Nope you need to define it only once, in app.js and in the ejs you can call this API using fetch function -- developers.google.com/web/updates/2015/03/introduction-to-fetch

          – Kiran Mathew Mohan
          Mar 26 at 8:16






          @HusseinMenshawi Nope you need to define it only once, in app.js and in the ejs you can call this API using fetch function -- developers.google.com/web/updates/2015/03/introduction-to-fetch

          – Kiran Mathew Mohan
          Mar 26 at 8:16














          I tried using the fetch function in my ejs file and i think its going through the API in app.js but, my record is not updating in the database like it is suppose to.

          – Hussein Menshawi
          Mar 27 at 10:57





          I tried using the fetch function in my ejs file and i think its going through the API in app.js but, my record is not updating in the database like it is suppose to.

          – Hussein Menshawi
          Mar 27 at 10:57













          @HusseinMenshawi Are you getting any errors in the console?

          – Kiran Mathew Mohan
          Mar 27 at 11:01





          @HusseinMenshawi Are you getting any errors in the console?

          – Kiran Mathew Mohan
          Mar 27 at 11:01













          i get a PUT 404 error,

          – Hussein Menshawi
          Mar 27 at 11:12





          i get a PUT 404 error,

          – Hussein Menshawi
          Mar 27 at 11:12



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304270%2faccessing-a-function-declared-in-app-js-in-an-ejs-file-using-nodejs-and-mongodb%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript