hello can anyone could help me ,when I run my code it doesn’t work I have the impression that my mongoose no longer wants to connect yet I’ve been connected since
this are my code
mongoose.connect(process.env['MONGO_URI'], { useNewUrlParser: true, useUnifiedTopology: true });
/** 2) Create a 'Person' Model */
var personSchema = new mongoose.Schema({
name: String,
age: Number,
favoriteFoods: [String]
});
/** 3) Create and Save a Person */
var Person = mongoose.model('Person', personSchema);
var createAndSavePerson = function(done) {
var janeFonda = new Person({name: "Jane Fonda", age: 84, favoriteFoods: ["eggs", "fish", "fresh fruit"]});
janeFonda.save(function(err, data) {
if (err) return console.error(err);
done(null, data)
});
};
/** 4) Create many People with `Model.create()` */
var arrayOfPeople = [
{name: "Frankie", age: 15, favoriteFoods: ["Del Taco"]},
{name: "Sol", age: 12, favoriteFoods: ["roast chicken"]},
{name: "Robert", age: 34, favoriteFoods: ["wine"]}
];
var createManyPeople = function(arrayOfPeople, done) {
Person.create(arrayOfPeople, function (err, people) {
if (err) return console.log(err);
done(null, people);
});
};
this is console
> node server.js
Your app is listening on port 3000
OPTIONS
POST
/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:847
const serverSelectionError = new ServerSelectionError();
^
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
at Connection.openUri (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:847:32)
at /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:351:10
at /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:1149:10)
at Mongoose.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:350:20)
at Object.<anonymous> (/home/runner/boilerplate-mongomongoose/myApp.js:3:10)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/home/runner/boilerplate-mongomongoose/server.js:67:16)
at Module._compile (node:internal/modules/cjs/loader:1159:14) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map(3) {
'ac-2lvmfyc-shard-00-00.guig8mn.mongodb.net:27017' => ServerDescription {
address: 'ac-2lvmfyc-shard-00-00.guig8mn.mongodb.net:27017',
error: MongoNetworkError: connection <monitor> to 34.199.61.99:27017 closed
at Connection.handleIssue (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:131:15)
at TLSSocket.<anonymous> (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:64:35)
at TLSSocket.emit (node:events:513:28)
at node:net:313:12
at TCP.done (node:_tls_wrap:587:7),
roundTripTime: -1,
lastUpdateTime: 70556777,
lastWriteDate: null,
opTime: null,
type: 'Unknown',
topologyVersion: undefined,
minWireVersion: 0,
maxWireVersion: 0,
hosts: [],
passives: [],
arbiters: [],
tags: []
},
'ac-2lvmfyc-shard-00-01.guig8mn.mongodb.net:27017' => ServerDescription {
address: 'ac-2lvmfyc-shard-00-01.guig8mn.mongodb.net:27017',
error: MongoNetworkError: connection <monitor> to 18.207.47.50:27017 closed
at Connection.handleIssue (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:131:15)
at TLSSocket.<anonymous> (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:64:35)
at TLSSocket.emit (node:events:513:28)
at node:net:313:12
at TCP.done (node:_tls_wrap:587:7),
roundTripTime: -1,
lastUpdateTime: 70556066,
lastWriteDate: null,
opTime: null,
type: 'Unknown',
topologyVersion: undefined,
minWireVersion: 0,
maxWireVersion: 0,
hosts: [],
passives: [],
arbiters: [],
tags: []
},
'ac-2lvmfyc-shard-00-02.guig8mn.mongodb.net:27017' => ServerDescription {
address: 'ac-2lvmfyc-shard-00-02.guig8mn.mongodb.net:27017',
error: MongoNetworkError: connection <monitor> to 18.207.36.39:27017 closed
at Connection.handleIssue (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:131:15)
at TLSSocket.<anonymous> (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:64:35)
at TLSSocket.emit (node:events:513:28)
at node:net:313:12
at TCP.done (node:_tls_wrap:587:7),
roundTripTime: -1,
lastUpdateTime: 70557556,
lastWriteDate: null,
opTime: null,
type: 'Unknown',
topologyVersion: undefined,
minWireVersion: 0,
maxWireVersion: 0,
hosts: [],
passives: [],
arbiters: [],
tags: []
}
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
Node.js v18.12.1
the link :
https://replit.com/@bowak/boilerplate-mongomongoose)
chalange link:
https://www.freecodecamp.org/learn/back-end-development-and-apis/mongodb-and-mongoose/create-many-records-with-model-create