0
const express = require('express');
var router = express.Router()
const app = express();
const pgp = require('pg-promise')();
const pug = require('pug');
const connect = require('../core/connect.js');
const model = require('../models/model.js');
app.get('/data',function (req, res)
{
  const data = model.getData(connect.db, 'Select * FROM items');
  res.send(data);
 });

Looking at the code here, I should be able to use the getData function from model because I required it within the actual get request to data. However for some reason, it does not seem to be performing the query. I have verified the data does exist through postgres. I also know that if I move the const data line out of the app.get, it also works.

Can someone please explain how I used my functions from the model.js file within GET and POST requests?

I'm not using Ajax or Jquery. I read through that article and I'm still not sure what exactly I'm missing. It is saying something about a callback, but I'm not too clear on that either.

Dolandlod
  • 21
  • 5
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – peteb Dec 03 '16 at 19:03
  • 2
    Once you have that "aha moment", and understand callbacks, everything will fall into place. It will require some effort, but I promise you it's worth it. Grok this: Synchronous expressions return a value. Asynchronous expressions execute a function that was passed in as an argument, and _**don't return a value**_. see: http://shichuan.github.io/javascript-patterns/#function-patterns – code_monk Dec 03 '16 at 19:34
  • also note: synchronous expressions block execution, making code very _readable_. asynchronous expressions do not block. execution continues right past them, even though they are not finished doing all their work, making code very _fast_. – code_monk Dec 03 '16 at 19:39
  • Even though you're probably right about the underlying cause of the issue, without the relevant code from `model.js` and possibly `connect.js`, it's just pure speculation. OP, please provide the relevant bits of those (at the very least the `getData` function). – jcaron Dec 03 '16 at 22:57
  • Sorry about delayed response. I did eventually figure out how to use callbacks and everything else. I had a serious misunderstanding of how promises work and executed code beyond the scope of the promise. Thanks for the help and great links. – Dolandlod Dec 29 '16 at 21:06

0 Answers0