Storing an object in a model using MongoDB and Express

I have created a user schema in mongoose with various fields such as userName, firstName, lastName, password, rate, and rates. I am trying to perform a PUT request to store a new key/value pair in the rates object. Before updating the rates, I check if it is correctly initialized and then proceed to update it with the new data. However, despite logging the values before and after the update, I encounter an issue where the updated rates are not being saved properly. Here is an example of the output: First put request: {} 'before' { '5686e0b40a8c0bc90a4fb4cc': '4' } 'update' In the next put request: {'5686e0b40a8c0bc90a4fb4cc': '4'} 'before' { '5686e0b40a8c0bc90a4fb4cc': '5' } 'update' Unexpectedly, I end up with: {} 'before' { '5686e0b40a8c0bc90a4fb4cc': '5' } 'update' The issue lies in the saving of user.rates using the code snippet: user.save(); I need assistance in resolving this matter efficiently.

Answer №1

The Object you are attempting to save is: { ObjectId : String}, while the field in the schema expects it to be {String : String}.

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

Do the vue/attribute-hyphenation default rule and vue/prop-name-casing conflict with each other?

I am currently working on a project using eslint-plugin-vue, where I have both child and parent components. The issue at hand is that the child component needs to pass a value into the parent component. // parent export default { name: 'recordDetail ...

The value retrieved from Axios is not properly set by React's useState function

My current challenge involves sending a GET request to http://localhost:8080/api/RE/template/${pageId} in order to display template items associated with a specific page ID. Upon checking the console, the following is displayed. https://i.sstatic.net/YCwe ...

Having trouble loading select2 using PHP/Ajax/JSON even though I can see the data in the inspector

I encountered a puzzling issue after deploying my web application from a Windows (XAMPP environment) to a Linux Server. Despite everything working perfectly on Windows, I am now facing a frustrating problem that has left me stumped. I have scoured through ...

Issue with Vue 2: Promise does not resolve after redirecting to another page

Although I realize this question might seem like a repetition, I have been tirelessly seeking a solution without success. The issue I am facing involves a method that resolves a promise only after the window has fully loaded. Subsequently, in my mounted h ...

Can VueJS lifecycle hooks be outsourced?

Is it possible to organize lifecycle hooks (like created / mounted) in a separate file for better simplicity and cleanliness? MyGreatView.vue import created from 'created.js' export default { created, // created() { console.log('vue Cre ...

Encountered error 'Cannot read property 'id' of undefined' while attempting to parse an array that was declared within a factory

I am currently developing a simple application (using MEAN web frameworks and node webkit) to enhance my understanding of angularjs. Below is the code snippet from my notificationFactory.js function notificationFactory() { var fooMessages = [ ...

HTML code displaying a fixed message at the top of the webpage

I'm working on a web page that has a lot of content. I need to have a message always visible at the bottom of the browser window, even when scrolling. Can anyone help me achieve this using simple CSS, HTML, jQuery, or PHP? Thanks! ...

What is the best way to include $rootScope in an AngularJS unit test?

Imagine a scenario where there is a service in my application that relies on a value stored in $rootScope. The example below shows a simple service that does this: angular.module('myServices', []) .factory('rootValGetterService', funct ...

JavaScript promises do not guarantee the execution of the loop

There was a block of Javascript code that I needed to modify. Initially, the code looked like this: if(!this.top3){ const promises = this.images.map((el, index) => { return this.getData(el.title, index); }); return Promise.all(promise ...

What is the best way to align table headers with their respective content?

Ensuring proper alignment of table columns with their respective headers is crucial. Consider an array of headers: const titles = ['name', 'lastname', 'age'] When displaying the table, the content may be like this: const cont ...

a guide to merging nested paths within an express framework

I am facing an issue with my path api/courses/materials where Express interprets the last 'materials' route as a parameter, causing an error: "CastError: Cast to ObjectId failed for value "materials" (type string) at path "_id" for model "courses ...

accessing the php script within a node environment

Hey there! I'm currently working on creating a chat system using socket.io, express.io, and node.js. So far, everything has been going smoothly as I've been following the documentation provided by these tools. However, when I attempt to integrat ...

Guide to iterating through a JSON object

Initially, I expected a simple question but it's proving to be more challenging than I thought. To provide some context, there is a JSON string returned from the server located within data.occupation. {"occupation": "Boxer", "id": 2},{"occupation": " ...

Aggregating MongoDB data - Grouping by date, even in the absence of records

I currently have a query structured as follows: {$match:{ "when":{$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 24 * 30)} }}, {$project:{ "year":{$year:"$when"}, "month":{$month:"$when"}, "day": {$dayOfMonth:"$when"} }}, ...

What is the best approach to modularizing the Yeoman generator that includes Bootstrap and Sass integration?

I am currently working on a project using Yeoman for Bootstrap and Sass auto-generation. My main challenge lies in restructuring the default project layout to enhance modularity. You can find the generator here on GitHub. The current structure of the appl ...

Discover the importance of Node.js integration with HTML

I am trying to display a Node-js value in an HTML file using the pug engine. In my app.js file: const express=require('express'); const app=express(); var bodyParser = require('body-parser'); app.set('views','views&apo ...

Tips for storing STL geometry in a cache outside of the STLLoader event listener

I am attempting to read and cache a geometry from an STL file using Three.js STLLoader. I am utilizing an event loop callback to retrieve the data (similar to the STLLoader example). My intention is to store it in an external variable called "cgeom". Howev ...

Breaking up the data returned by an ajax call

I'm facing a bit of a challenge with what seems like a simple task. I am attempting to split the value returned by my ajax function, but I believe I may be specifying the return value incorrectly. The code in question is provided below. <script&g ...

Show where the image should be placed according to the coordinates of the mouse click

My project begins with a blank canvas, prompting the user to click anywhere on the page to reveal an image. My goal is to show an image at the exact location where the user clicks. Although I can successfully trigger an image to appear in the corner of the ...

What is the best way to eliminate excess elements from overflow:hidden?

Whenever I click on a modal, it changes my body to overflow:hidden. As a result, the scrollbar disappears and my page becomes unscrollable. Once I close the modal, my body reverts back to overflow:auto allowing the page to scroll again. These functionaliti ...