Unhandled rejection MongoError: Unsupported OP_QUERY command: insert. The client driver may require an upgrade. For more details see https://dochub.mongodb.org/core/legacy-opcod
e-removal …how do i solve that? please
Can you please show the code you wrote which caused the issue?
The link is not working for me so, can you fix that as well?
I searched and found this: https://www.mongodb.com/docs/manual/legacy-opcodes/
is this what you meant to share?
By the way:
can you use this and execute again and share with us how it goes:
try {
// your code that is causing issue
}
catch (ex) {
console.log('something must be wrong', ex);
// share with us the output of this in the console.
}
Thanks!
hi and thank for answering, it happens to me in the rentals.js file
Here is what I know.
Fawn is a library to help you create transactions (a concept where the all the database operations should either succeed or fail) in MongoDB.
I tried to use Fawn but couldn’t use it either because it’s outdated and probably there is no workaround to fix this (just my opinion).
I am not a MongoDB expert either (just good with MySQL only).
What I suggest is that, don’t use Fawn library just to create a transaction. Get the concept and later explore MongoDB concept for Two Phase Commit.
MongoDB introduced the concept of transactions in v4. Mosh was using Fawn at the time as MongoDB didn’t have transactions. Don’t use Fawn as it is outdated, use transactions as shown in the snippet below
const { Rental, validate } = require("../models/rental");
const { Customer } = require("../models/customer");
const { Movie } = require("../models/movie");
const _ = require("lodash");
const mongoose = require("mongoose");
const express = require("express");
const router = express.Router();
router.post("/", async (req, res) => {
const { error } = validate(req.body);
if (error) return res.status(400).send(error.details[0].message);
const customer = await Customer.findById(req.body.customerId);
if (!customer) return res.status(400).send("Invalid customer.");
const movie = await Movie.findById(req.body.movieId);
if (!movie) return res.status(400).send("Invalid movie.");
if (movie.numberInStock === 0)
return res.status(400).send("Movie not in stock.");
const session = await mongoose.startSession();
try {
session.startTransaction();
const rental = await Rental.create(
[
{
customer: _.pick(customer, ["_id", "name", "phone", "isGold"]),
movie: _.pick(movie, ["_id", "title", "dailyRentalRate"]),
},
],
{ session }
);
await Movie.findByIdAndUpdate(
movie._id,
{ $inc: { numberInStock: -1 } },
{ session }
);
await session.commitTransaction();
res.send(rental);
} catch (error) {
await session.abortTransaction();
res.status(500).send("Something went wrong.");
}
session.endSession();
});
You can find this on the Mongoose docs on how to use transactions. Note that I’m using Model.create() to create a new document and putting the new document object inside an array for me to pass the options on the create method as per the docs.