Can someone help me with this warning?

DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

const winston = require('winston');
const mongoose = require('mongoose');
const config = require('config');

module.exports = function() {
  const db = config.get('db');

  mongoose.connect(db, {useNewUrlParser: true})
    .then(() => winston.info(`Connected to ${db}...`));
}

don’t worry this problem is already fixed. I added

mongoose.set(‘useCreateIndex’, true);

This is a MongoDB error. Check the documentation.

thanks! yes it happened when I upgraded the version.

Becareful…$set will create/overwrite an existing Index.

https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#db.collection.createIndex

https://docs.mongodb.com/manual/reference/operator/update/set/

These aren’t the same.

so you are saying I should use:

collection.ensureIndex();

It depends on the use case. If you want to receive an handle an error if the index already exists I would use collection.ensureIndex(); if you want to clobber the data use $set if you want to update data use set the upsert flag.

Just wanted you to be aware of the difference.

image

in my case I don’t have a catch() in my connect promise so I guess is set then? what does clobber mean? sorry hispanic speaker here :frowning:

I don’t think clobber is a mongodb term. It just means to replace all the data/overwrite.

I actually have a

Cannot GET /

in localhost:3900 is this due to

mongoose.set(‘useCreateIndex’, true); ?

No, idea. That’s a very vague error. It could be that whatever your GET is pointed to is not a valid path.

Do you have this in a Github repository? If you can share your code we might be able to help.

yes here is my repo https://github.com/Ceci007/vidly-backend the problem is in the movies.js router, all started to crash when I tried to add image upload functionality for this I changed the movie.js model and the movies.js router and now my app is not working, I tried postman and get, post and put have errors, they have trouble with the movieImg: req.file.path that’s what I’m getting. :frowning: I’ll wait patiently if someone could help me this weekend please.

I’ll see if I can look at this later unless someone else can get to it.

no it’s ok I can wait, later is fine :slight_smile:

Can you give me some information so I make sure to configure the mongodb correctly?

What is the DB and table names?

I think I found it in the code but just want to make sure I have the same environment.

the db is vidly and it has :

genres:
movies:
users:

example of genre

_id:objectId(“5d5b1e79ef1bff31543d09db”)
name: “Comedy”
__v:0

example of movie

_id:objectId(“5d5b1e79ef1bff31543d09dc”)
title: “Airplane”
numberInStock:5
dailyRentalRate:2
genre:object
_id:objectId(“5d5b1e79ef1bff31543d09db”)
name:“Comedy”
movieImg:“”
__v:0

ok so right now what I have is the node server running with your cloned repository…

$ nodemon index.js
[nodemon] 1.18.11
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index.js`
info: Listening on port 3900...
info: Connected to mongodb://localhost/vidly...

Mongodb is listening on 27017

2019-08-23T10:16:39.600-0500 I NETWORK  [initandlisten] waiting for connections on port 27017

I have a DB named ‘vidly’ and a table named ‘users’.

> show dbs
admin          0.000GB
config         0.000GB
local          0.000GB
vidly          0.000GB
> use vidly
switched to db vidly
> show tables
users

The server isn’t crashing. What’s the next step?

I have a 404 error in postman in the get request http://localhost:3900

Deprecation means something it is obsolete and it had been replaced by something else.

In your case it was ensureIndex and recommend that you use createIndex.

This error might not be you who trigger it. It could be the library you are using.