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

Does this still work? #4

Open
OKNoah opened this issue Feb 3, 2016 · 7 comments
Open

Does this still work? #4

OKNoah opened this issue Feb 3, 2016 · 7 comments

Comments

@OKNoah
Copy link

OKNoah commented Feb 3, 2016

Using mongoose version 4.3.7 and node 4.2.4, this does not seem to work at all. Gives no errors either.

Model:

TweetSchema.plugin(supergoose, { instance: mongoose })

Controller:

Tweet.findOrCreate({ url }, (err, tweet, created) => {
  console.log(err)
})
@romanmt
Copy link
Contributor

romanmt commented Feb 3, 2016

@OKNoah What is the value of url in your example?

@OKNoah
Copy link
Author

OKNoah commented Feb 4, 2016

@romanmt That's shorthand for url: url. There's a const url = 'http://... above that. If I do console.log(url) it shows me the string I expect.

@romanmt
Copy link
Contributor

romanmt commented Feb 4, 2016

@OKNoah.

I'm using node 4.2.4 and mongoose 4.4.1 (however I know I've used this project with earlier versions of both). The case you describe above is covered in the following test.

 it("should notify when object was created", function(done) {
    Click.findOrCreate({ip: '127.0.0.1'}, function(err, click, created) {
      created.should.eql(true)
      done()
    })
  })

I ran this test and it passes. Please take a look at the test file to see if there's anything you might be missing. If you're still having trouble post more of your controller method and model so I can try to reproduce.

@mnebuerquo
Copy link

I was experiencing the same error, I think, in this except from a document save method in my project:

MetaTag.findOrCreate({tagName:tag}, {sources:[self.source]}, function(err,tagrecord,created){
    if(err){
        return next(err);
    }
    MetaTag.update({_id:tagrecord._id},{'$addToSet': {sources: self.source}});
    tagrecord.myTags.forEach(function(tag){ newtags[tag]=1 });
    return next();
});

The symptom was that when I would call findOrCreate(...) the callback would never get called. The program would hang, waiting forever for that callback.

The problem as I later identified was that I had done a refactor and moved some stuff out to a separate init function. My call to mongoose.connect(config.db.url); was not being called prior to findOrCreate so it didn't have a database connection to use.

I think an improvement would be to detect this condition and call the callback with an error argument indicating that there was no available connection to make the query. It's kind of an obvious thing to check for when debugging, but it's nice when the library helps us when we're stupid.

(Sorry about the double comment and delete. I was logged in with the wrong account.)

(Also, just for your info, I started out by using mongoose-findorcreate which I found on npm. His is a fork of yours, but has had no updates in a while, and has no issue tracker to report issues. I found supergoose when I noticed that his was a fork. His has more downloads per month, but you have an issue tracker, so I think there is someone listening when I want to comment. Thanks for listening!)

@romanmt
Copy link
Contributor

romanmt commented Apr 23, 2016

@mnebuerquo Thanks for the comment. I'm glad you were able to find a solution. You're suggestion is valid. I'd like for this library to be more robust. I'll be away for the next week so It will be a while before I can implement the missing connection error handling. However if you would like to implement it before then, feel free. Pull requests are always welcomed, and I'll be sure to add you to the contributors list.

@rakesh1988
Copy link

@mnebuerquo thanks for pointing this out. I was also doing the same and always got the error as below

TypeError: user.findOrCreate is not a function

When I tried to include console.log on user model and the db-helper(this is the file that initiates connection to mongodb), i found that user model was created first and then the connection was open. Is there any way I can delay creation of user model until the connection is established?
Any help/pointers are highly appreciated.

@AfaGaming
Copy link

hey all, I know this issue was 7 years ago but does this npm package still work?

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

5 participants