Error message when running app.js

 throw new error_1.MongoParseError(`${optionWord} ${Array.from(unsupportedOptions).join(', ')} ${isOrAre} not supported`);

This error is gen whenever I go to start up my server for testing

const express = require("express");
const app = express();
const morgan = require("morgan");
const bodyParser = require("body-parser");
const mongoose = require('mongoose');

//sets names for the different areas of the projects
const productRoutes = require('./api/routes/products');
const orderRoutes = require('./api/routes/orders');

mongoose.connect('mongodb+srv://24williamharris:' + 
process.env.MONGO_ATLAS_PW + 
'@cluster0.pxgod.mongodb.net/Cluster0?retryWrites=true&w=majority', 
{
    useMongoClient: true
});

//Logs stuff in terminal
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

//Prevents Cors Errors
app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*")
    res.header(
        "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Authorization"
    );
    if (req.method === 'OPTIONS'){
        res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
        return res.status(200).json({});
    }
    next();
});

//routes the requests to other areas of the project
app.use('/products', productRoutes);
app.use('/orders', orderRoutes);

app.use((req, res, next) => {
    const error = new Error('Not found');
    error.status = 404;
    next(error);
});

app.use((error, req, res, next) => {
    res.status(error.status|| 500);
    res.json({
        error: {
            message: error.message
        }
    });
});

module.exports = app;

Here is my source code for the app.js file

I wasn’t getting it a bit earlier, then I started adding things to the project with mongoose, and I cant find where the issue keeps originating from.

Anyone on this forum see where my issue lies?

Hi @William_Harris1,

I guess one of these because there are no MongoDB queries in there so no parsing and this seems to be an error about parsing. I’m not big in Node so not sure if I can assist further…

I at the moment only have one point of implementation in the /products, can you look at my code here and see what the issue is?

router.post('/', (req, res, next) => {
    const product = new Product({
        _id: new mongoose.Types.ObjectId(),
        name: req.body.name,
        price: req.body.price
    });
    product.save().then(result =>{
        console.log(result);
    });
    res.status(201).json({
        message: 'Handling POST requests to /products',
        createdProduct: product
    });
});

Oops, I forgot these parts of it all, my apologies

contents of product.js in models folder

const mongoose = require('mongoose');

const productSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    name: String,
    price: Number
});

module.exports = mongoose.model('Product', productSchema);

Import method for product.js


const Product = require('../models/product');

Issue found, the method used to connect is not used in Mongoose 6, Downgrading to 5 seems to work.

1 Like