tnkh
tnkh

Reputation: 1839

Meteor: insert failed: Access denied. No allow validators set on restricted collection for method 'insert'

Hello I am working on a Meteor-React project and fairly new in this. I am trying to insert new object into existing collection but it shows error as indicated in the title.

This is my component responsible for inserting new item at the UI level.

import React, { useState, useEffect } from "react";
import newTask from '../api/create'

export default Create = () => {
  const [task, createTask] = useState('');
  handleKeyPress = (event) => {
    if(event.key === 'Enter'){
      newTask(task)
    }
}
return (
<form className="add-task" noValidate="">
  <div>
    <div className="fieldset add-task-input fieldset-stripped">
      <div className="fieldset-content">
        <label className="fieldset-label">
          <span className="fieldset-label-content has-icon">
            <i className="icon-plus" />
          </span>
          <input
            className=""
            name="title"
            placeholder="Add new task"
            type="text"
            autoComplete="off"
            value={task}
            onChange={(e) => { createTask(e.target.value)}}
            onKeyPress={this.handleKeyPress}
          />
        </label>
       </div>
     </div>
   </div>
  </form>
 )
}

This is the method that I am trying to export and import into the component file above.

import { Tasks } from '../../tasks';

export default newTask = (taskTitle) => Tasks.insert({
  title: taskTitle,
  dueDate: null,
  repeat: {},
  color: '#4e42c3',
  status: 'incomplete',
  customFields: []
})

I have tried using methods proposed by others as below, by adding the code below into the method file above:

Tasks.allow({
  insert: function () {
    return true;
  }
})

But still it does not work and show the same error. Any idea how to enable inserting new item into the Mongo Collection ?

Upvotes: 1

Views: 980

Answers (3)

Jankapunkt
Jankapunkt

Reputation: 8423

The allow/deny is opening a security vulnerability. You may rather use it only for prototyping / mocking some initial software prototypes.

The correct way to update collections is to use Meteor methods:

server

import { Tasks } from '../path/to/tasks'

Meteor.methods({
  'insertTask': function ({ title, dueDate, repeat, color, status, customFields }) {
    return Tasks.insert({ title, dueDate, repeat, color, status, customFields })
  }
})

client

import { Tasks } from '../../tasks';

export default newTask = (taskTitle) => Meteor.call('insertTask', {
  title: taskTitle,
  dueDate: null,
  repeat: {},
  color: '#4e42c3',
  status: 'incomplete',
  customFields: []
})

Edit: If you only want to insert the document at the UI level and never have a collection on the server stored, you may make the Task collection local by exporting it wihtout a name:

client

export const Tasks = new Mongo.Collection(null)

Upvotes: 1

tnkh
tnkh

Reputation: 1839

So previously I added the code below

Tasks.allow({
  insert: function () {
    return true;
  }
})

at the client side. Once I moved it to server side, it solves my problem.

Upvotes: 2

Bharath Bheemireddy
Bharath Bheemireddy

Reputation: 1

Are you using autopublish package? Your trying to insert the object from the client side. Without autopublish package you can't do it. Either add autopublish or insert from the server side.

Upvotes: 0

Related Questions