What is this Mongoose CursorNotFound Error? - mongodb

I have mongoose throwing this error from time to time, it's not stopping the node application from running but I can't find any information about whats causing the error and I would like to fix it.
The link to the repo is here https://github.com/bkawk/divergence2
I have found I can stop the error by adding a limit to the records here
https://github.com/bkawk/divergence2/blob/master/src/db.js#L94
{ MongoError: cursor id 31871209567 not found
at /Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:598:61
at authenticateStragglers (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:516:16)
at Connection.messageHandler (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:552:5)
at emitMessageHandler (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:309:10)
at Socket.<anonymous> (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:488:15)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:607:20)
name: 'MongoError',
message: 'cursor id 31871209567 not found',
ok: 0,
errmsg: 'cursor id 31871209567 not found',
code: 43,
codeName: 'CursorNotFound' }

Related

GitLab pipeline - postgres connection termined unexpectedly

I am having a little trouble with postgres in docker .. I am running this pipeline in gitlab ... and everything starts fine.. I start a postgres:14-alpine docker image and run unit tests of the application ... and when it's about to start (create the test database) ...
I tried increasing the timeouts
pool: {
max: 50,
min: 5,
idle: 600000, // 10min
acquire: 600000,
createTimeoutMillis: 600000,
acquireTimeoutMillis: 600000,
idleTimeoutMillis: 600000,
reapIntervalMillis: 600000,
createRetryIntervalMillis: 600000
}
but it keeps ending up with this "Connection terminated unexpectedly"
Creating database db_unittests, owner will be postgres.
Executing (default): CREATE DATABASE db_unittests WITH OWNER = postgres;
/builds/project/node_modules/sequelize/lib/dialects/postgres/query.js:386
return new sequelizeErrors.DatabaseError(err);
^
DatabaseError [SequelizeDatabaseError]: Connection terminated unexpectedly
at Query.formatError (/builds/project/node_modules/sequelize/lib/dialects/postgres/query.js:386:16)
at Query.run (/builds/project/node_modules/sequelize/lib/dialects/postgres/query.js:87:18)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async /builds/project/node_modules/sequelize/lib/sequelize.js:619:16
at async run (/builds/project/tests/scripts/database-recreate.js:50:3) {
parent: Error: Connection terminated unexpectedly
at Connection.<anonymous> (/builds/project/node_modules/pg/lib/client.js:132:73)
at Object.onceWrapper (node:events:627:28)
at Connection.emit (node:events:513:28)
at Socket.<anonymous> (/builds/project/node_modules/pg/lib/connection.js:107:12)
at Socket.emit (node:events:525:35)
at endReadableNT (node:internal/streams/readable:1358:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
sql: 'CREATE DATABASE db_unittests WITH OWNER = postgres;',
parameters: undefined
},
original: Error: Connection terminated unexpectedly
at Connection.<anonymous> (/builds/project/node_modules/pg/lib/client.js:132:73)
at Object.onceWrapper (node:events:627:28)
at Connection.emit (node:events:513:28)
at Socket.<anonymous> (/builds/project/node_modules/pg/lib/connection.js:107:12)
at Socket.emit (node:events:525:35)
at endReadableNT (node:internal/streams/readable:1358:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
sql: 'CREATE DATABASE db_unittests WITH OWNER = postgres;',
parameters: undefined
},
sql: 'CREATE DATABASE db_unittests WITH OWNER = postgres;',
parameters: undefined
}
Locally, it all works fine .. only in the pipeline - it acting up :disappointed:
Any idea why is that so? Thanks a lot

Error: MongoError: bad auth: Authentication failed

You might be trying to tell me to "change my password" or "remove brackets, these <>"
I have done both. I have made sure that the password I'm using is correct.
Here is the URI:
mongodb+srv://user:pass#notlbot.iclv0tm.mongodb.net/?retryWrites=true&w=majority
no, my username isnt 'user' and my password also is not 'pass'
I don't know what to do from here, though it may be important to note I am trying to connect from a Linodes server. I'm using AWS to host Atlas.
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at MessageStream.Writable.write (node:internal/streams/writable:334:10)
at TLSSocket.ondata (node:internal/streams/readable:754:22)
at TLSSocket.emit (node:events:527:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at TLSSocket.Readable.push (node:internal/streams/readable:228:10)
at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
ok: 0,
code: 8000,
codeName: 'AtlasError'
}

Heroku app fails to connect to mongodb atlas server (after migration from mlab)

I recently migrated from mlab to mongodb atlas due to its deprecation. I managed to successfully get a connection to the db and tested it with the mongoDB compass but when I add the environment variable to heroku it gives me the following error message:
MongoNetworkError: failed to connect to server [cluster-grwbxfbx-shard-00-02.e6jtl.mongodb.net:27017] on first connect [MongoNetworkError: connection 4 to cluster-grwbxfbx-shard-00-02.e6jtl.mongodb.net:27017 closed
2020-11-11T23:53:44.726144+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:372:9)
2020-11-11T23:53:44.726144+00:00 app[web.1]: at Object.onceWrapper (events.js:421:26)
2020-11-11T23:53:44.726145+00:00 app[web.1]: at TLSSocket.emit (events.js:314:20)
2020-11-11T23:53:44.726145+00:00 app[web.1]: at net.js:676:12
2020-11-11T23:53:44.726146+00:00 app[web.1]: at TCP.done (_tls_wrap.js:568:7)]
2020-11-11T23:53:44.726147+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
2020-11-11T23:53:44.726147+00:00 app[web.1]: at Pool.emit (events.js:314:20)
2020-11-11T23:53:44.726148+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:562:14
2020-11-11T23:53:44.726149+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:1009:9
2020-11-11T23:53:44.726149+00:00 app[web.1]: at callback (/app/node_modules/mongodb/lib/core/connection/connect.js:97:5)
2020-11-11T23:53:44.726150+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/connect.js:124:7
2020-11-11T23:53:44.726150+00:00 app[web.1]: at _callback (/app/node_modules/mongodb/lib/core/connection/connect.js:349:5)
2020-11-11T23:53:44.726151+00:00 app[web.1]: at Connection.errorHandler (/app/node_modules/mongodb/lib/core/connection/connect.js:365:5)
2020-11-11T23:53:44.726151+00:00 app[web.1]: at Object.onceWrapper (events.js:421:26)
2020-11-11T23:53:44.726151+00:00 app[web.1]: at Connection.emit (events.js:314:20)
2020-11-11T23:53:44.726152+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:370:12)
2020-11-11T23:53:44.726152+00:00 app[web.1]: at Object.onceWrapper (events.js:421:26)
2020-11-11T23:53:44.726153+00:00 app[web.1]: at TLSSocket.emit (events.js:314:20)
2020-11-11T23:53:44.726153+00:00 app[web.1]: at net.js:676:12
2020-11-11T23:53:44.726153+00:00 app[web.1]: at TCP.done (_tls_wrap.js:568:7)
2020-11-11T23:53:44.812696+00:00 heroku[web.1]: Process exited with status 0
2020-11-11T23:53:44.850554+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-11T23:53:44.854049+00:00 heroku[web.1]: State changed from crashed to starting
2020-11-11T23:53:47.149749+00:00 heroku[web.1]: Starting process with command `npm start`
2020-11-11T23:53:49.907225+00:00 app[web.1]:
It seems as if heroku is adding a port to the end of the MONGODB_URI. Does anyone know whats going on?
my config var in heroku is:
MONGODB_URI = mongodb+srv://username:password#cluster-grwbxfbx.e6jtl.mongodb.net/test?retryWrites=true&w=majority
with username and password being replaced by my admin username and password.
Also here is my code for connecting to the db
import mongoose from 'mongoose'
import options from '../config'
export const connect = (url = options.dbUrl, opts = {}) => {
return mongoose.connect(url, { ...opts, useNewUrlParser: true })
}
export const config = {
secrets: {
jwt: 'learneverything'
},
dbUrl: process.env.MONGODB_URI || 'mongodb://localhost:27017/api-design'
}
Let me know if you need me to post anything else

Google OAuth2 callback error

Very, very new to user authentication. Looking to allow users to register with Google.
Current status: I'm successfully making it to this step -
(hiding my email(s))
But, when I click an account, I'm getting this error (500) (using longjohn for longer stack traces):
Error
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-google-oauth20/lib/strategy.js:95:21
at passBackControl (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:132:9)
at IncomingMessage.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:157:7)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:188:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
---------------------------------------------
at IncomingMessage.Readable.on (_stream_readable.js:689:35)
at ClientRequest.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:156:14)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at HTTPParser.parserOnIncomingClient (_http_client.js:522:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at TLSSocket.socketOnData (_http_client.js:411:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at TLSSocket.Readable.push (_stream_readable.js:136:10)
at TLSWrap.onread (net.js:560:20)
---------------------------------------------
at exports.OAuth2._executeRequest (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:147:11)
at exports.OAuth2._request (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:120:8)
at exports.OAuth2.get (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:227:8)
at Strategy.userProfile (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-google-oauth20/lib/strategy.js:84:16)
at loadIt (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:345:17)
at Strategy.OAuth2Strategy._loadUserProfile (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:360:25)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:168:16
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:209:7
at passBackControl (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:134:9)
at IncomingMessage.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:157:7)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:188:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
---------------------------------------------
at IncomingMessage.Readable.on (_stream_readable.js:689:35)
at ClientRequest.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:156:14)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at HTTPParser.parserOnIncomingClient (_http_client.js:522:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at TLSSocket.socketOnData (_http_client.js:411:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at TLSSocket.Readable.push (_stream_readable.js:136:10)
at TLSWrap.onread (net.js:560:20)
---------------------------------------------
at exports.OAuth2._executeRequest (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:147:11)
at exports.OAuth2._request (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:120:8)
at exports.OAuth2.getOAuthAccessToken (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:190:8)
at loaded (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:164:20)
at NullStore.verify (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/state/null.js:9:3)
at Strategy.OAuth2Strategy.authenticate (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:210:26)
at attempt (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport/lib/middleware/authenticate.js:348:16)
at authenticate (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport/lib/middleware/authenticate.js:349:7)
at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:335:12)
at next (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:174:3)
at router (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:317:13)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:335:12)
---------------------------------------------
at handleOperationCallback (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:454:24)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:490:9
at authenticateStragglers (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:429:16)
at Connection.messageHandler (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:463:5)
at Socket.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:319:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at Socket.Readable.push (_stream_readable.js:136:10)
at TCP.onread (net.js:560:20)
My code surrounding this functionality is:
// routes.js
router.get('/auth/google', authController.google);
router.get('/oauth2callback', authController.googleCallback
);
// passport.js
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CONSUMER_KEY,
clientSecret: process.env.GOOGLE_CONSUMER_SECRET,
callbackURL: "http://localhost:21015/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
const user = new User();
user.google.id = profile.id;
user.google.token = accessToken;
user.google.name = profile.displayName;
user.google.email = profile.emails[0].value;
user.save(function(err) {
if (err) throw err;
return done(null, user);
});
}
));
// authController.js
const passport = require('passport');
exports.google = passport.authenticate('google', {
scope: 'https://www.google.com/m8/feeds'
});
exports.googleCallback = passport.authenticate('google', {
failureRedirect: '/login', // if fail, where to go
failureFlash: 'Failed Login!', // if fail, what to flash
successRedirect: '/', // if success, where to go
successFlash: 'You are now logged in!' // if success, what to flash
});
Apparently you can not use an IP address as and end-point, so try to change your callback-url to : http://localhost:21015/oauth2callback
Make sure you have enabled Google+ API in https://console.cloud.google.com.
To use google Auth you have to enable the API in google console. A very common mistake is when you go to Google Developer Console to enable an API for your project, first time for a project it asks you to create Credentials. The problem is after you create credentials the google+ API (or any other APIs) won't be enabled automatically and you should enable it from your project page in google developer console.
STEPS:
Go to Google Console
Select your project from the top nav (if you didn't yet)
Click on Enable APIS and Services
In the search box search for Google Plus
Select the first result , Google+ API
Click Enable
If you don't see the Enable button and see the 'Create Credentials' button,it means you didn't create credentials yet and follow the instructions on the page to create one. Don't forget to enable API after creating credentials.
For more information you can read this instruction from google Enable and disable APIs
Besides what mentioned above, you don't need to use an absolute URL for your callback and simply you can use a relative one like: /auth/google/callback
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CONSUMER_KEY,
clientSecret: process.env.GOOGLE_CONSUMER_SECRET,
callbackURL: "/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
const user = new User();
user.google.id = profile.id;
user.google.token = accessToken;
user.google.name = profile.displayName;
user.google.email = profile.emails[0].value;
user.save(function(err) {
if (err) throw err;
return done(null, user);
});
}
));

Parse Query where near geopoint error

So I have posted my issue here. In short, I can't query on Geopoint on my parse-server I get a strange error:
{"code":1,"message":"Internal server error."} (Code: 1, Version: 1.14.4)
Optional("{"code":1,"message":"Internal server error."}")
On the dashboard logs it says something a bit more complex:
2017-05-08T04:06:47.283Z - Uncaught internal server error. { MongoError: error processing query: ns=heroku_r6dg7cvc.CommentaryTree: $and
_created_at $gt new Date(1494130007253)
_rperm $in [ null "*" "a81FrGp2R3" ]
GEONEAR field=location maxdist=7.17619e-07 isNearSphere=1
Sort: {}
Proj: {}
planner returned error: unable to find index for $geoNear query
at Function.MongoError.create (/app/node_modules/mongodb-core/lib/error.js:31:11)
at /app/node_modules/mongodb-core/lib/connection/pool.js:489:72
at authenticateStragglers (/app/node_modules/mongodb-core/lib/connection/pool.js:435:16)
at Connection.messageHandler (/app/node_modules/mongodb-core/lib/connection/pool.js:469:5)
at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:321:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at Socket.Readable.push (_stream_readable.js:136:10)
at TCP.onread (net.js:561:20)
name: 'MongoError',
message: 'error processing query: ns=heroku_r6dg7cvc.CommentaryTree: $and\n _created_at $gt new Date(1494130007253)\n _rperm $in [ null "*" "a81FrGp2R3" ]\n GEONEAR field=location maxdist=7.17619e-07 isNearSphere=1\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
waitedMS: 0,
ok: 0,
errmsg: 'error processing query: ns=heroku_r6dg7cvc.CommentaryTree: $and\n _created_at $gt new Date(1494130007253)\n _rperm $in [ null "*" "a81FrGp2R3" ]\n GEONEAR field=location maxdist=7.17619e-07 isNearSphere=1\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
code: 2 } MongoError: error processing query: ns=heroku_r6dg7cvc.CommentaryTree: $and
_created_at $gt new Date(1494130007253)
_rperm $in [ null "*" "a81FrGp2R3" ]
GEONEAR field=location maxdist=7.17619e-07 isNearSphere=1
Sort: {}
Proj: {}
planner returned error: unable to find index for $geoNear query
at Function.MongoError.create (/app/node_modules/mongodb-core/lib/error.js:31:11)
at /app/node_modules/mongodb-core/lib/connection/pool.js:489:72
at authenticateStragglers (/app/node_modules/mongodb-core/lib/connection/pool.js:435:16)
at Connection.messageHandler (/app/node_modules/mongodb-core/lib/connection/pool.js:469:5)
at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:321:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at Socket.Readable.push (_stream_readable.js:136:10)
at TCP.onread (net.js:561:20)
After searching for a while, its possible it might be related to this.
Where the solution would be the same to the second link, but I don't know how to apply the solution to a parse-server on heroku.
You using the mlab extension on Heroku? Go to MLab and add an index for the geo field on your collection. You don't need to use an API for this, use the MLab dashboard.