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

Cannot read property 'createQuery' of undefined #10

Closed
deltreey opened this issue Aug 22, 2016 · 9 comments
Closed

Cannot read property 'createQuery' of undefined #10

deltreey opened this issue Aug 22, 2016 · 9 comments

Comments

@deltreey
Copy link

As a user of mongoose, wanting to start switching to google cloud datastore, this library is ideal for me, thank you so much for making it.

So I created a connection:

var gstore = require('gstore-node');
var gcloud = require('google-cloud')(config.googlecloud);
gstore.connect(gcloud.datastore());

Then I created a schema and made it into a model:

var gstore = require('gstore-node');
var Schema = gstore.Schema;

var urlSchema= new Schema({
    name: { type: 'String', excludeFromIndexes: true },
    subdomain: { type: 'String' }
});

module.exports = gstore.model('url', urlSchema);

I don't know what step I might be missing in the docs, but when I try to query my new model:

var Url = require('./url.model');
Url.query().run(function (error, results) {
    console.error(error);
    console.log(results);
});

I get the above error (it doesn't even make it to the console.error line. Specifically, it points to line 924 of model.js in gstore-node:

return self.ds.createQuery.apply(self.ds, createQueryArgs);

For some reason, the datastore is supposed to be passed in when creating the model, but is not. Given that there is a line 924 of model.js in the lib/ folder or gstore-node, at this point I'm pretty sure I'm in over my head and could use some guidance.

Did I do something wrong in the process of creating my model? Note that if I dump the Url object, it has a ds property with a datastore. object on it. In fact, if I dump the self object in model.js, It contains the Url complete with the attached datastore, but for some reason self.ds is undefined.

@sebelga
Copy link
Owner

sebelga commented Aug 23, 2016

Humm.. that's strange. I just double checked the Url.query().run(...) to make sure it was not broke but it works on my side. Can you double check the version of gstore-node (v0.6.1) and google-cloud (v0.38.3)

Try also this for the connection:

var ds = gcloud.datastore();
gstore.connect(ds);

@deltreey
Copy link
Author

From the package.json:

"google-cloud": "0.38.3",
"gstore-node": "0.6.1"
> node -v
v5.1.1
> npm -v
3.5.1

if it will help, I can make an sscce.

@deltreey
Copy link
Author

tested with your minor change:

var ds = gcloud.datastore();
gstore.connect(ds);

It made no difference

@sebelga
Copy link
Owner

sebelga commented Aug 23, 2016

I am currently working on a demo app still in active development but have a look at how the controller and model are declared. Check main.js (root folder), blog-post.model.js and blog-post.controller.js

https://github.com/sebelga/blog-app-googlecloud/tree/develop

Your initial code seems ok and it's hard to debug without an example.

@deltreey
Copy link
Author

my error is in my config. Instead of passing the file name to the keyFilename property, I was requiring the file and passing the json to it.

{
    projectId: 'project_id',
    keyFilename: 'file.json'
}

@deltreey
Copy link
Author

on further analysis, this error is related to external files creating & using models without the benefit of the existing datastore instance created on the gstore-object. This is something you'll see a lot in express code. The model in an external file is fine, until you import it in your controller where the gstore object was never declared. Then things start to fall apart.

@deltreey
Copy link
Author

deltreey commented Aug 24, 2016

I'm re-opening this as a bug.

The way mongoose works, you don't need a complete connection to create a model. In gstore-node, I need to create the datastore object/connection in my model files so that when I pull in the models elsewhere they work acceptably.

Here's a demo of what I'm talking about. Creating a model before creating a connection is what breaks everything.

var cloudJson = require('./cloud.json');
var gstore = require('gstore-node');
var Schema = gstore.Schema;

var urlSchema = new Schema({
    name: { type: 'String', excludeFromIndexes: true },
    subdomain: { type: 'String' }
});

var Url = gstore.model('url', urlSchema);

 var configGcloud = {
    projectId: cloudJson.project_id,
    keyFilename: 'cloud.json'
};
var gcloud = require('google-cloud')(configGcloud);
var ds = gcloud.datastore();

gstore.connect(ds);

Url.query().run(function (err, urls) {
    if (err) { return console.error(err); }
    console.log(urls);
});

@deltreey deltreey reopened this Aug 24, 2016
@sebelga
Copy link
Owner

sebelga commented Aug 25, 2016

I see, you're right. I just changed the order of my require (putting the models declaration first) and I reproduced the bug. I will have a look into it. Thanks for reporting!

sebelga added a commit that referenced this issue Aug 26, 2016
@sebelga
Copy link
Owner

sebelga commented Aug 26, 2016

I just released a version that should fix this problem. I am closing this issue, feel free to come back if the problem persists.

@sebelga sebelga closed this as completed Aug 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants