Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgp undefined in utility functions when using repo pattern #647

Closed
kissmygritts opened this issue Aug 28, 2019 · 4 comments
Labels

Comments

@kissmygritts
Copy link

@kissmygritts kissmygritts commented Aug 28, 2019

Expected behavior

pgp to be defined in utility functions that I am attempting to use in a repo, as described in the repo example you have in the pg-promise-demo.

Actual behavior

pgp is undefined. I think this has to do with the order modules are initialized when starting a server.

Steps to reproduce

standard set up as described in pg-promise-demo

create a utils/index.js file

// util index.js
const { pgp } = require('./../db')

const pagination = limit => {
// limit is a single number
  return pgp.as.format('LIMIT $1', limit)
}

Then alter the the all method in db/repo/products.js

const { pagination } = require('./../../util')
... other code

async all(limit) {
  return this.db.any('SELECT * FROM products $1:raw', pagination(limit))
}

When attempting to run this method the follow error is returned:

"TypeError: Cannot read property 'as' of undefined"

I can get around this by require pgp as a parameter for the pagination function, as this is then passed in from the Products class. Alternatively I can require the pg-promise library as mentioned here.

I suppose I'm mostly just curious about this behavior. I understand why it is happening (because the repos are being initialized before db and pgp are initialized, and therefore before the utility functions are). What are your thoughts? Should I require the pg-promise library in my utility functions? Or provide pgp as a parameter to my utility functions?

I apologize if this isn't truly a pg-promise issue. Any help is much appreciated.

Environment

  • Version of pg-promise: 8.7.4
  • OS type (Linux/Windows/Mac): Mac
  • Version of Node.js: 10.16.3
@vitaly-t

This comment has been minimized.

Copy link
Owner

@vitaly-t vitaly-t commented Aug 28, 2019

@kissmygritts

The steps you provided are not enough to reproduce the issue. Try the following:

  • fork pg-promise-demo, and add the code necessary to see the issue, then post the link here.
@kissmygritts

This comment has been minimized.

Copy link
Author

@kissmygritts kissmygritts commented Aug 28, 2019

Link to my fork with a working example. https://github.com/kissmygritts/pg-promise-demo

Under the Users.all method are three different return statements. The first is from the pg-promise-demo modified with a utility function. This will return the error above. Comment out this error and use either of the other return statements and the proper data will be returned (in this case, 2 users instead of 5).

@vitaly-t

This comment has been minimized.

Copy link
Owner

@vitaly-t vitaly-t commented Aug 28, 2019

You are creating a loop-reference:

The last line results in including the first one. So from the last inclusion's perspective, const pgp doesn't get a chance to finish its initialization before you start using it.

That's a coding mistake on your part, it is not an in issue with this library, so I'm closing it.

@kissmygritts

This comment has been minimized.

Copy link
Author

@kissmygritts kissmygritts commented Aug 29, 2019

Got it. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.