Unable to get Mongoose's Required field to work in conjunction with Enum validation

Encountering issues with Mongoose Required true and Enum validation when using updateone

await MonthlyTarget.updateOne({website: req.body.website, year: req.body.year, month: req.body.month}, req.body, {upsert: true});

Model

'use strict';
import pkg from 'mongoose';
const { Schema, model } = pkg;

const monthlyTargetSchema = new Schema({
    website: { type: Schema.Types.ObjectId, ref:'Websites', index: true, required: true },
    category: { type: Schema.Types.ObjectId, ref:'IncidentCategory', index: true, required: true },
    year: { type: String, index: true },
    month: { type: String, required: true, enum:['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'], index: true },
    target: { type: Schema.Types.Number, index: true },
});

const MonthlyTarget = model('MonthlyTarget', monthlyTargetSchema);
export default MonthlyTarget;

Answer №1

 let updatedUserData = await User.updateOne({ _id: req.params.id},{ $set: newData },{ runValidators: true });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

`Stop the JW Player from playing several videos simultaneously`

I have a situation where I want to display multiple videos on the same page, but only one should be playable at a time. When a new video is started, the currently playing video should automatically stop. <div class="tab-pane active" id="abc"><d ...

Use jQuery to drag an element and display controls in a designated area

I am trying to develop a tool that allows me to drag an image from one section to another, and upon dropping it, the following actions should take place: 1 - The dragged image should revert back to its original position 2 - A set of controls should be add ...

Transforming the AngularJS $http GET method to OPTION and including custom headers

var users= $resource('http://myapp.herokuapp.com/users', {}); users.get(); The change in the HTTP GET method to OPTION occurred after implementing a header method. var users= $resource('http://myapp.herokuapp.com/users', {}, { get ...

Tips for handling Promise.all and waiting for all promises to resolve in an async function in Express JS

I'm relatively new to JavaScript and especially asynchronous programming. My current project involves creating an Express+React application that shows a GitHub user's information, including a few repositories with the latest 5 commits for each. ...

Arranging information extracted from an XML document following an ajax request

Here is a snippet of XML data sample to work with: <?xml version="1.0" encoding="ISO-8859-1"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>U ...

Efficiently Manipulating Arrays in JavaScript

After reading a .csv file and saving it to an array, I encountered the following array structure: var data = [["abc;def"],["ghi;jkl"], ...] The strings within the nested arrays are separated by semicolons. In order to work with this da ...

What is the mechanism behind the jQuery ready function that enables its first parameter to transform into a jQuery object?

While browsing today, I came across this interesting code snippet: jQuery(document).ready(function($$){ console.log($$); }); It seems that $$ is recognized as the jQuery object itself. Typically, to achieve this, we would need to pass the jQuery varia ...

A comprehensive guide on organizing JavaScript files within an Angular project

In the process of building my MEAN app, I have structured my folders in the following way: https://i.sstatic.net/hR7xN.png I have included a js file in /angular/src/assets/js for jQuery functionalities. To achieve this, I utilized npm to install jQuery. ...

The JavaScript and CSS properties are not functioning properly with the HTML text field

I came across this CodePen example by dsholmes and made some modifications: Here Furthermore, I have developed my own form on another CodePen pen: Link The issue I'm facing is related to the placeholders/labels not disappearing when typing in text f ...

Is it possible to use both interfaces and string union types in TypeScript?

My goal is to create a method that accepts a key argument which can be either a string or an instance of the indexable type interface IValidationContextIndex. Here is the implementation: /** * Retrieves all values in the ValidationContext container. ...

Displaying content conditionally in Vue JS upon the initial page load

I am currently working on a Vue component that is supposed to render the first time a page is opened. However, I am facing some confusion regarding the logic behind this. My approach involves using localStorage to check for an item called 'wasVisited& ...

Express - Retrieve data from one endpoint and send it to another destination

There is a post method in my code that calculates something and returns it using the res.json method. This is how the code looks: app.post("/calc", function (req, res) { res.json({result: 100}); }); Now, I want to retrieve that JSON data from another ...

Iterate through the loop to add data to the table

Looking for a way to append table data stored in local storage, I attempted to use a loop to add cells while changing the numbers based on row counts. Here is my JavaScript code snippet: $(function() { $("#loadTask").change(function() { var ta ...

Looking to obtain rendered tree data upon page load?

Is there a way to retrieve all rendered tree metadata when the page loads using jstree? I'm looking for an output similar to this: [23,24] Please note that I would like to have each id stored in the metadata object displayed as [23,24] upon page loa ...

Use Google Maps to plan your route and find out the distance in kilometers as well as the

Feeling a bit overwhelmed with the project I'm working on, but hoping for some guidance. We're aiming to create a form where users input a starting point and an ending point, similar to the examples on Google Maps (http://code.google.com/apis/ma ...

"Changing background color, incorporating hover effects, utilizing !important, and manipulating .css properties with

Encountered a dilemma. I devised a "tabs" feature illustrated in the following demo: http://jsfiddle.net/4FLCe/ The original intention was for the tab to change color to A when hovered over, and to color B when clicked on. However, as shown in the demo, ...

Is there a way I can retrieve a cookie stored in an Express middleware even if the req.cookies object is returning empty?

I am currently working on validating a cookie using the cookie-parser module to verify if the user is authenticated to access restricted routes within my application. My technology stack includes NodeJS and Express for the server, and Sveltekit for the fro ...

The directives applied within ng-include are not functioning correctly

Below is a custom directive I have implemented. var fancySelectDirective = pluginDirecitvesModule.directive("custom-select",function(){ return { restrict: 'C', link: function (scope, element, attrs) { ...

Flot seems to be having difficulty uploading JSON files

I am relatively new to working with json and flot, but have been tasked with creating a chart. Can someone please help me troubleshoot why my code is not functioning as expected? $.getJSON('chart.json', function(graphData){ alert(graphData); ...

Having trouble sending a FormData object with Axios for a user uploaded file in a React, Node.JS project

My goal is to enable users to upload images to my application, which consists of a React frontend and a Node backend. The process involves sending the uploaded file from the client to the server, followed by making a request from the server to Firebase clo ...