Guide on Validating and Updating an embedded item within a mongoDB Collection Document

How do I go about updating the values of an embedded object within a mongoDB document?

The values displayed for {{service.id}} and {{service.username}} in the table template are correct. However, I am unsure of the correct way to access them within the saveItem() function. When attempting service.id, service.$.id, and service.[0].id, I encounter an Error: Unexpected token .. Similarly, using "service.id" results in a submission with no outcome, while "service.[0].id" gets the form stuck in edit mode. Lastly, "service.$.id" throws an error indicating that the $ field is not updateable.

Could there be something missing in my JavaScript code, or am I possibly mishandling the schema definition (i.e., unnecessary use of dollar signs)?

Thank you!

Here is the code snippet:

var Schemas = {};

Items = new Meteor.Collection('items');

Schemas.Items = new SimpleSchema({
  _id: {
    type: String,
  },
  name: {
    type: String,
    label: "Item Name",
    min: 1
  },
  "service.$": {
    type: [Object]
  },
  "service.$.id": {
    type: Number
  },
  "service.$.username": {
    type: String
  }

});

Items.attachSchema(Schemas.Items);
...


Answer №1

Found the solution thanks to the guidance of @MichelFloyd.

Here is the recommended schema format: (excluding the use of $ signs)

 "product.$": {
    type: [Object]
  },
  "product.$.id": {
    type: Number
  },
  "product.$.name": {
    type: String
  }

By following this structure, the code will function correctly:

 var newItem = {
   _id: $('#newId').val(),
   title: $('#newTitle').val(),
  "product.id": $('#newProductId').val(),
  "product.name": $('#newProductName').val()
}

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

Ensuring type signatures are maintained when wrapping Vue computed properties and methods within the Vue.extend constructor

Currently, I am trying to encapsulate all of my defined methods and computed properties within a function that tracks their execution time. I aim to keep the IntelliSense predictions intact, which are based on the type signature of Vue.extend({... Howeve ...

Issue with VueJs and ChartJs not displaying custom options when making an API call

Having trouble populating my chart with data from the API. Despite setting extraOptions for my chart, it defaults to default options when rendered. Here is the component code: import { Bar, mixins } from 'vue-chartjs'; export default { name: ...

Conceal the .dropdown-backdrop from bootstrap using solely CSS styling techniques

Is there a way to hide the .dropdown-backdrop element from Bootstrap for a specific dropdown on a webpage using only CSS? I found a solution that involves Javascript, you can view it on JSFiddle here. However, I am hoping to achieve this without relying o ...

Problem identified with Vue.js: The Log in screen briefly flashes before redirecting the authenticated user (resulting in a full page refresh)

My routing is functioning properly, utilizing navigation guards to prevent users from accessing the login or register routes once they are signed in. However, when I manually type '/auth/signin' in the address bar, the login screen briefly appear ...

Can the ValidationPipe be utilized with a whiteList on the response body?

How can we prevent the return of certain key values in responses from a NestJs server when using TypeOrm entities? For instance, ensuring that a user's password is never sent to any client: In the user.entity.ts file: @Entity() export class User ext ...

JQuery Autocomplete Error: Unable to access property 'value' of undefined

I am currently utilizing a jquery autocomplete plugin found here. However, I am encountering an issue when I click on a filtered result, triggering the following error: Uncaught TypeError: Cannot read property 'value' of undefined Upon inspecti ...

"Use the data-attribute to dynamically append a child element to the

Here are a couple of divs that look like this: <div id="thisisme" data-order="1"> <div id="thisisme" data-order="2"> I am looking to insert some content into the specific div with data-order = 1. The following Javascript code is what I have t ...

Error: The variable "message" has not been defined. Please define it before displaying the welcome

While I was experimenting with my welcome message and attempting to convert it into an embed, I ended up rewriting the entire code to make it compatible. However, upon completion, I encountered the error message is not defined. var welcomePath = './ ...

Ways to conceal images until AFTER the completion of the jquery flexslider loading process

After trying to integrate wootheme's Flexslider on my website, I encountered a small issue with its loading process. Whenever the page is refreshed with the slider, there is a brief moment (approximately 1 second) where the first slide appears overly ...

Spontaneous Link with JQuery and Colorbox

Just to clarify, I have no programming experience so please explain everything in simple terms. I am currently using a .js script within SharePoint and it's working perfectly! <!DOCTYPE html> <html> <head> <meta charset= ...

Issue with iframe's size not displaying correctly

<body> <script> document.write("<iframe id='theframe' src='http://www.website.com?testvr=" + testvr + " height='900' width='500'></iframe>"); </script> </body> The iframe is added, but ...

The tooltip feature is functioning properly on the button, but unfortunately it is not working

I am incorporating Tooltips into my project, utilizing Vue 3 and Bootstrap 5. In the script section, I have included the following: <script> import { Tooltip } from "bootstrap/dist/js/bootstrap.esm.min.js"; export default { mounte ...

"Exploring the creation of multidimensional arrays in Arduino - what steps should I

Is there a way to create a multidimensional array in Arduino? I want something like this. C++ var arr = { name: "John", age: "51", children: [ "Sara", "Daniel" ] }; or maybe like this. JSON ...

Allow for the ability to choose a specific option for every individual line that is echoed in

I have researched several similar questions, but none of them address exactly what I am attempting to achieve. My goal is to use AJAX to fetch a PHP page that will display the contents of a folder on my server. Currently, the files are being listed line by ...

Contrast between using " and '

Similar Inquiry: When to Utilize Double or Single Quotes in JavaScript Comparison of single quotes and double quotes in JS As I delve into creating a Node.js Express web application, I've noticed that the tutorial consistently uses ' ins ...

Spring Boot fails to recognize path variable data sent from Angular

When working with Angular 7, I encountered a situation where I needed to pass a value from the service class of Angular. Here is how I achieved it: executeHelloWorldBeanServiceWithPathVariable(name){ console.log("name coming from here"+name); retu ...

Error: Authentication Error - Headers have already been sent to the client and cannot be modified

I am currently working on handling authentication errors for my website. However, when I submit incorrect data, I encounter the following error: node:internal/errors:478 ErrorCaptureStackTrace(err); ^ Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers aft ...

Adding a fresh element to an array in Angular 4 using an observable

I am currently working on a page that showcases a list of locations, with the ability to click on each location and display the corresponding assets. Here is how I have structured the template: <li *ngFor="let location of locations" (click)="se ...

Switch out the ajax data in the input field

Is there a way to update the value in a text box using Ajax? Below is my code snippet: <input type="text" id="category_name" name="category_name" value="<?php if(isset($compName)) { echo ucfirst($compName); ...

What is the method for calculating the mean of two values stored in separate objects?

Here is the data I have: [ { "_id": ObjectId("6213baa5a013b7c5f1232e23"), "birthDate": ISODate("1973-05-01T13:30:15Z"), "surname": "Johnson", "name": "Emma", ...