developerg1000
developerg1000

Reputation: 121

mongoose insertOne node.js failing on insert

I have this model, defining my mongoose schema and such:

const servicesSchema = new mongoose.Schema({
    cnum: {
        type: String,
        required: true,
        unique: true,
        trim: true
    },
    location: {
        type: String,
        required: true,
        trim: true
    },
    name: {
        type: String,
        required: true,
        trim: true
    }
}, {
    timestamps: true
})

//force collection name
servicesSchema.set('collection', 'services');

servicesSchema.methods.toJSON = function () {
    const servicesObject = this.toObject()

    return servicesObject
}

const Services = mongoose.model('Services', servicesSchema)

module.exports = Services

then, in my route, I have this:

const ServicesModel = require('../model/services')
app.post('/updateServices', (req, res) => {
    //defining variables passed through from front-end
    var siteLocation = req.body.siteLocation;
    var Services = req.body.Services;
    var date = req.body.date;
    var cnum = req.body.cnum;
    console.log(siteLocation, Services, date, cnum)

  
        //defining parameters to insert into document
        const doc = {
            siteLocation: siteLocation,
            Services: Services,
            date: date,
            cnum: cnum
        };

        console.log(doc)
        //actually inserting document into collection.
        ServicesModel.insertOne(doc)

        //redirecting to set page or function
        res.status(200).send("success")
})

but, when I try to insert (the insertone), it tells me in my console that TypeError: ServicesModel.insertOne is not a function.

I am wondering what the problem is. keep in mind, I have never used mongoose before and never set it up like this, so apologies in advance. thanks for the help

Upvotes: 1

Views: 539

Answers (1)

developerg1000
developerg1000

Reputation: 121

Figured it out.

Mongoose doesn't support insertOne

you must use create

Upvotes: 2

Related Questions