Federica Meloni
Federica Meloni

Reputation: 11

Find and print elements name with MongoDB and Mangoose with Node.js then disconnect to the server

I'm using mongoDB with mongoose I created a fruit collection and I want to print the fruits names then disconnect to the server

const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/fruitsDB", {
  useNewUrlParser: true
});

const fruitSchema = new mongoose.Schema({ //schema: come vogliamo che gli eleemnti vengano sturtturati
  name: String,

  rating: {
    type: Number,
    min: 1,
    max: 10
  },
  review: String
});

//creiamo un modulo
const Fruit = mongoose.model("Fruit", fruitSchema);

const fruit = new Fruit({
  name: "Apple",
  rating: 8,
  review: "good"
});
//fruit.save();
const kiwi = new Fruit({
  name: "Kiwi",
  rating: 10,
  review: "great"
});

Fruit.find(function(err, fruits) {
  if (err) {
    console.log(err);
  } else {
    mongoose.connection.close();
    fruits.forEach(function(fruit) {
      console.log(fruit.name);
    });
  }
});

I've written this in the app.js then I compiled it with the command "node app.js" The terminal gives me this:

Apple Apple Apple Apple Kiwi

(node:24352) UnhandledPromiseRejectionWarning: MongoExpiredSessionError: Cannot use a session that has ended at Object.applySession (C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\sessions.js:647:16) at Connection.command (C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection.js:185:36) at C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\sdam\server.js:176:18 at Object.callback (C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection_pool.js:266:13) at C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection_pool.js:474:29 at C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection_pool.js:402:13 at callback (C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connect.js:52:9) at C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connect.js:124:13 at MessageStream.messageHandler (C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection.js:479:9) at MessageStream.emit (events.js:400:28) (Use node --trace-warnings ... to show where the warning was created) (node:24352) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:24352) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Upvotes: 0

Views: 136

Answers (2)

Le Rilleur
Le Rilleur

Reputation: 245

You can try something like that:

const fruits = await Fruit.find({})
// do whatever you want with fruits
mongoose.connection.close();

It looks like you are trying to close the connection while still using it. By using await you're gonna wait that the query is over before closing the connection.

Upvotes: 0

Ayzrian
Ayzrian

Reputation: 2465

First of all you need to save your models to database first.

Second of all looks like you are using the find method in wrong way.

const fruit = new Fruit({
  name: "Apple",
  rating: 8,
  review: "good"
});

fruit.save();

const kiwi = new Fruit({
  name: "Kiwi",
  rating: 10,
  review: "great"
});

kiwi.save();

Fruit.find({}).then((fruits) => {         
    mongoose.connection.close();
    fruits.forEach(function(fruit) {
      console.log(fruit.name);
    });
});

Also I would suggest to use async/await to improve code readability.

Upvotes: 0

Related Questions