Skip to content

Conversation

@IAMOTZ
Copy link

@IAMOTZ IAMOTZ commented Feb 28, 2020

Hi there!
I was trying out this mongoose plugin some days ago and I figured one of the code samples in the README is not working as expected. This PR attempts to fixe the issue.

The Issue:
There is this code sample in the README:

Click.create({ip: '127.0.0.1'}, {browser: 'Mozilla'}, function(err, val) {
  Click.findOrCreate({ip: '127.0.0.1'}, {browser: 'Chrome'}, function(err, click) {
    console.log('A click from "%s" using "%s" was found', click.ip, click.browser);
    // prints A click from "127.0.0.1" using "Mozilla" was found
  })
});

When I run this locally, it prints: A click from "127.0.0.1" using "undefined" was found. I figured the first query inserts two different documents({ip: '127.0.0.1'} and {browser: 'Mozilla'}) in the DB, while the second query does nothing since the document matching {ip: '127.0.0.1'} already exists.

The fix:
I updated the code sample to use Click.findOrCreate for the first query:

Click.findOrCreate({ip: '127.0.0.1'}, {browser: 'Mozilla'}, function(err, val) {
  Click.findOrCreate({ip: '127.0.0.1'}, {browser: 'Chrome'}, function(err, click) {
    console.log('A click from "%s" using "%s" was found', click.ip, click.browser);
    // prints A click from "127.0.0.1" using "Mozilla" was found
  })
});

When I run this, it prints the expected message: A click from "127.0.0.1" using "Mozilla" was found. The first query inserts just a single document (merging the first and second argument), while the second query still does nothing since the document matching {ip: '127.0.0.1'} already exists.

@IAMOTZ
Copy link
Author

IAMOTZ commented Mar 16, 2020

Not sure why the build is failing, I only touched the README file.

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

Successfully merging this pull request may close these issues.

2 participants