Node seed.js error in vidly-api-node - MongoError: Unsupported OP_QUERY command: delete

Mmm I dont think so, I just cloned the repository form moshs github profile, but now that i think i solved this error before Cannot install dependencies for vidfly-api-node - #5 by rollingtatoo22

I’m trying to downgrade the mongo version but it is a little difficult jajaj

1 Like

sorry i have not had time yet to try it. But, i think i tried changing the versions when i got the error and still didnt work, but honestly i was already giving up on it hhaahah, i will try again as soon as i can. Meanwhile, I hope you will find out

1 Like

Well, for me it doesn’t work at all - I was using node v16.16.0, downgrade it to 4.8.7 and then I have different error when using node seed.js:

Strange thing is - I tried to install mongod version 5 and installation ends succesfully, but still mongod --version show me that I’m using version 6.0 o_O

I finally make it worked!!! So - I try to debug it with my coworker and the problem was moongose - according to this docs (Mongoose v6.5.2: MongoDB Version Compatibility) MongoDB with version 6.0 works with mongoose version ^6.5.0. I had version 5.* so this mongo client was not compatible with it. After upgrading mongoose to ^6.5.2 it finally works and I see in compass this vildy db :slight_smile:

2 Likes

Hello, how can I update mongoose ? I’m searching for documentation but I can’t find anything. Sorry if the question is silly

No question is silly :slight_smile: You can just use this in the vidly-ap-node folder:
npm install --save mongoose@6

Unfortunately this works only for letting me seed this file to my Mongo Compass app :confused: I still got new issue with using node index.js :confused: But I try to consult it again.

Thankss, I already solve it, now we are in the same problem again jajajaj

Hey did you solve the other problem? if yes please tell me how

1 Like

vidly-api-node:

Verified with node 14.18.1.

Make the following changes:
In package.json

“bcrypt”: “^5.0.0”
“mongoose”: “^6.5.2”

In rentals.js:

//Fawn.init(mongoose);
Fawn.init(“mongodb://127.0.0.1:27017/vidly”);

rm -rf node_modules
npm i
node seed.js
node index.js

PS:

2 Likes

Works for me - just to clear up: it’s rentals.js in routes folder (there’s similar file rental.js in models and I was confused for a few minutes :D).

1 Like

That’s right. Updated!

Hi
thanks for ur answer. I did all the things you said.
i am able to run seed.js but when i want to run index.js it get this error:
‘SyntaxError: Invalid or unexpected token’
at Fawn.init(“mongodb://127.0.0.1:27017/vidly”);

image

The quotes seem to be incorrect when it is copied from browser and directly pasted into vscode.
Correct the quotes as shown, and it should work.

thank u so much it finally worked!

Thanks so much guys, you saved my 2 weeks at least for fixing this :smile:

Just change the version of MongoDb. For me v5 has worked. Problem is node.js and mongodb version confliction.

plz guid me how did u solve it

just uninstall your current mongodb and install version 3.6.2 it will solve your problem

Hello everyone,
I solved the issue by (trying to do a kind of two phase commit to avoid the OP_QUERY issue). Can you help me on my code, it’s working but I’m a beginner:
const {Rental, validate} = require(‘…/models/rental’);
const {Movie} = require(‘…/models/movie’);
const {Customer} = require(‘…/models/customer’);
const mongoose = require(‘mongoose’);
// const Fawn = require(‘fawn’);
const express = require(‘express’);
const router = express.Router();

// Fawn.init(“mongodb://127.0.0.1:27017/vidly”);
router.get(‘/’, async (req, res) => {
const session = await mongoose.startSession();
const rentals = await Rental.find().sort(‘-dateOut’);
res.send(rentals);
session.endSession();
});

router.post(‘/’, async (req, res) => {
const session = await mongoose.startSession();
const { error } = validate(req.body);
if (error) return res.status(400).send(error.details[0].message);

const customer = await Customer.findById(req.body.customerId);
// .select(“-isGold”);
if (!customer) return res.status(400).send(‘Invalid customer.’);

let movie = await Movie.findById(req.body.movieId);
if (!movie) return res.status(400).send(‘Invalid movie.’);
session.startTransaction();
if (movie.numberInStock === 0) return res.status(400).send(‘Movie not in stock.’);

let rental = new Rental({
customer: {
_id: customer._id,
isGold : customer.isGold,
name: customer.name,
phone: customer.phone
},
movie: {
_id: movie._id,
title: movie.title,
dailyRentalRate: movie.dailyRentalRate
}
});

const myrental = await Rental.findOne({“customer._id”: customer._id,“movie._id”:movie._id });

if (!myrental ) {
rental = await rental.save();
movie.numberInStock–;
movie = await movie.save();}
else {
console.log(myrental.movie._id);
session.abortTransaction();
return res.status(400).send(‘you have already rent the movie’);}

session.commitTransaction();
session.endSession();
res.send(rental);

});

router.get(‘/:id’, async (req, res) => {
const rental = await Rental.findById(req.params.id);

if (!rental) return res.status(404).send(‘The rental with the given ID was not found.’);
session.endSession();
res.send(rental);

});

module.exports = router;

it’s working on my side, if the connection is cut and the rental is already in the db, it will abort the transaction