Is it not possible to simultaneously edit multiple records within a collection?

Every time I encounter this issue:

Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403]

and here is the relevant code snippet:

Template.notifications.events({
   'click a.clearAll':function(event){
    console.log("hey");
    Notifications.update({{userId:Meteor.userId()},{$set:{read:true}},{multi:true});
 }
})

I have already configured update permissions as follows:

Notifications.allow({
  insert: function(){
 return true;
 },
 update: function(){
return true;
 }
});

What could be causing the restriction on updates?

Answer №1

The issue at hand arises due to the fact that you are attempting the update from the client side, which is classified as 'untrusted code' by the server.

Unless you eradicate the insecure package and establish proper permission rules, the process will not go smoothly. I highly recommend removing the insecure package using meteor remove insecure and ensuring that all updates are executed through server initiated methods/calls.

You must initiate a 'call' from the client to the server and implement the update command within the methods function.

client:

Template.notifications.events({
   'click a.clearAll':function(event){
        Meteor.call('updateDocs',
            function(error, result) {
                if (error) {
                    console.log(error);
                }
                else {
                    console.log('done');
                }
            });
    }
});

server:

Meteor.methods({
    updateDocs: function() {
        var userId = this.userId
        if (userId) {
            Notifications.update({{userId: userId},{$set:{read:true}},{multi:true});
        }
        return;
    }
});

Answer №2

Hats off to @meteorBuzz for providing the accurate solution. In case you are interested in updating from the client side, you have the option to utilize forEach:

Notifications.find({ userId:Meteor.userId() }).forEach(function(n){
  Notifications.update({ _id: n._id },{$set:{ read: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

Incorporate a new node module into your Ember CLI application

I am interested in integrating the Node.js module https://www.npmjs.com/package/remarkable-regexp into my Ember-CLI project. Can someone guide me on how to make it accessible within the Ember application? I attempted to do so by including the following c ...

Find what you're looking for by exploring the search results inside the text box marked

update1: Appreciate your response! However, I am facing an issue where typing 'h' displays a set of values. When selecting a value, it should appear in the text box with a cross symbol, similar to how tags are edited on StackOverflow. I am work ...

Adding a prefix to the imported CSS file

My React app, created with create-react-app, is designed to be integrated as a "widget" within other websites rather than functioning as a standalone application. To achieve this, I provide website owners with minified JS and CSS files that they can inser ...

Understanding Mongodb: the process of populating a schema that is referenced within another schema using an API

Looking to make adjustments to my Api in order to populate a referenced schema. Here's the schema I am working with: export const taskSchema = new Schema ({ user:{ type: String, required: true }, project: { type ...

Looking to convert an empty star glyphicon button into a star glyphicon button when clicked using JavaScript?

Is there a way to make the glyphicon change when clicking on the button instead of the glyphicon itself? <script> $('.btn').click(function(){ $(this).find('.glyphicon').toggleClass('glyphicon-star-empty glyphico ...

What is the process for assigning a random string value to each document within a mongodb collection using the mongo shell?

Looking to assign a randomly generated string property to every item in a MongoDB collection. Planning to leverage the mongo shell and the updateMany function for a swift and efficient solution. ...

Sorting and filtering arrays of embedded documents with MongoDB's multikey index for efficient min/max key comparisons

Example Simplified for Clarity To keep things straightforward, let's dive right into an example: Imagine a collection named foo with a multikey index {searchTags: 1}: {_id: 1, searchTags: [{bar: "BAR"}]} {_id: 2, searchTags: [{baz: "B ...

Personalizing MaterialUI's autocomplete functionality

Trying to implement the material-UI Autocomplete component in my react app and looking for a way to display a div when hovering over an option from the list, similar to what is shown in this reference image. View example If anyone has any tips or suggest ...

Discovering which dependencies rely on the eval function can be done by examining

I've been working on a chrome extension, but I keep encountering an error that mentions the use of eval. Surprisingly, my code doesn't include eval, so it must be coming from one of my dependencies. Is there a quick and efficient method to pinpoi ...

Modify the background color of a div by selecting a hex code from a dropdown menu

Is there a way to utilize jQuery in order to alter the background of a <div> by choosing the HEX code from a separate dropdown menu? HTML <select id="target"> <option value="#c2c2c2">Grey</option> <option value="blue">Bl ...

Verify whether the user's email is registered in the database

I have a login form and I want to verify if a user's email exists in the database, but I'm not sure about the syntax for integrating Angular with Node. In my Main.js file, I have an ng-click on the submit button that triggers this function. I ex ...

Utilize Javascript to establish a fresh attribute by analyzing other attributes contained in an array within the object

Currently, I am working on a data structure that looks like this: masterObject: { Steps: [ { step: { required: false, }, step: { required: false, }, step: { required: false, }, }, ] } ...

Using Vue.js with the slot-in attribute

Looking to create a unique vue.js template that functions as described below: <CustomComponent></CustomComponent> should produce <div class="a"></div> <CustomComponent>b</CustomComponent> should result in <div class ...

The validation of fields using form.validateFields() may not function properly when utilizing a customized Ant Design form component

When looking at the example below, we are encountering an issue with creating custom components inside forms using antd4 version. const handleFormSubmit = () => { form .validateFields() .then((values: any) => { console.log('succe ...

Arrange data in JSON file based on job title (role name) category

My current code successfully outputs data from a JSON file, but I'm looking to enhance it by organizing the output based on the "Role Name". For example, individuals with the role of Associate Editor should have their information displayed in one sect ...

Encountering an unusual issue while implementing collision detection with THREE.Raycaster in version r68

Up until now, I've successfully utilized the THREE.Raycaster in my game engine for testing collisions across various elements. It has been reliable and effective. However, a puzzling issue has recently arisen that has left me stumped. Despite believi ...

Navigating to the bottom of a page once an element is displayed (with react-slidedown)

I'm facing a bit of a challenge here that I haven't been able to resolve yet. So, I have this optin form that is revealed upon clicking on a link. The reveal effect is achieved using react-slidedown. The issue I'm encountering is that when ...

Searching by array type in MongoDB is a breeze

I need to search my mongoDB collection by type. Let's say I have these two entries in the hello collection: { "_id" : ObjectId("56684ee0f597654b99d0d636"), "name" : "Scrooge", "surname" : "McDuck", "address" : { ...

Looking to integrate MongodbJobStore into your Java web application project using the quartz.properties file for connecting to MongoDB?

I am having trouble configuring the quartz.properties file to connect to MongoDB. Here is my quartz.properties file: #specify the jobstore used org.quartz.jobStore.class=com.novemberain.quartz.mongodb.MongoDBJobStore org.quartz.jobStore.mongoUri=mongodb:/ ...

Is it better to return JSON before streaming a file through an iframe or sending HTTP headers?

I have created a form that utilizes a hidden iframe to submit a file to a script which then modifies the file and returns the altered version. I have realized that I can avoid saving the file anywhere by simply using echo file_get_contents(tmp);, where tmp ...