Meteor Collection Insert Fail - insert failed: method not found


#1

I know there are multiple ways to insert information into a Mongo collection, however I wanted to insert it through the JavaScript file every time I submit a form. Unfortunately, every time I do submit the form, the JavaScript console on the browser prints ‘insert failed: method not found’. Here is my code in the .js file:

Test = new Mongo.Collection('test');

if (Meteor.isClient) {

Template.Test.events({
	'submit form': function(event) {
		event.preventDefault();
		var firstName = event.target.first.value;
		
		Test.insert({
			name: firstName,
			createdAt: new Date()
		});
	}
});
}

if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup

     });  
}


//  This is my HTML code:
 <template name="Test">
 <form>
 	<p>First name <input class="firstTest" type="name" name="first"></p>
	<input class="saveBtn" type="submit" value="Save">
 </form>
</template>

All I really want to do, is store user information upon their registration. I really don’t understand why I am running into this problem. Do I need a specific package? What exactly do I have to do to make this work? Would anyone be able to give an hand. Please and thank you in advance. I would really appreciate it.


#2

Can you please provide the full error trace? Any error message in the server console?


#3

This what I got:


#4

In the browser:


#5

When I press the ‘Save’ button (above) in theory it should insert the information into the collection but instead I get this error (bellow).


#6

To prove it didn’t work, I looked into the collection and I get this (bellow)


#7

I decided to look into Mongo to see if it would tell me something, but nothing (at least I think so)

.

Please let me know if you need more information, I would be more than happy to provide (just be specific). I really need to find a solution to this, in order to finish this project. Please and once again, thank you in advance.

I apologize for the multiple reply posts; meteor only lets me post a image per post.


#8

The most likely cause of this is that you have defined your collection (Test) on the client only. Is the js code you originally provided in a file in the client/ folder?


#9

Hey, Robfallows

Yes, it is. Do I have to create a serve folder and place the .js file with the collection in there?


#10

Where exactly should I place it? Thank you


#11

As your code is written (using Meteor.isServer and Meteor.isClient) you can if you wish place this in a shared location. In any event, the Test = new Mongo.Collection('test'); must be shared (the “usual” place is in lib/).

By all means split your code appropriately between lib/, client/ and server/. There are some good patterns for this: look first in the “Organizing Your Project” section of the docs. If you put your code in these directories you can generally dispense with using Meteor.isServer and Meteor.isClient in your code.


#12

Thank you so much, I did that and it worked! I really appreciate it.


#13

Folders are very important in Meteor, not only for functionalities but also (and mostly) for security.

Here is the part of the manual describing how it works:

http://docs.meteor.com/#/full/structuringyourapp

May I highly recommend you to read at least the “Concepts” part of this manual. Not reading this will expose your code to the most basic security flaws and you won’t get how Meteor actually works until then.


#14

Hi everyone, I am new to meteor.while do practice I have got a problem…
I Unable to insert data from browser.


#15

This is my main.js file


#16

and data in mongodb

But In browser the data is not displaying…

please help me to resolve this issue.Thanks a ton. :slight_smile:


#17

From what you’ve said it looks like you have not defined the collection on the server as well as on the client.

As you are using the 1.3 import syntax, here’s what I would do:

Create a new file /imports/api/Names.js and add the following into that file (note I used the Mongo.Collection syntax - Meteor.Collection has been deprecated for a couple of years):

export const Names = new Mongo.Collection('names');

In your client/main.js remove Names = new Meteor.Collection('names'); and add

import { Names } from '/imports/api/Names';

to the import statements at the top.

Add the same import statement to the top of your server/main.js file. This ensures the collection is defined on the server as well.

A final comment: if you are basing this off the initial “Welcome to Meteor” app which is created for you with meteor create ..., your code is already split into client-only (under client/) and server-only (under server/)parts. Code in client/ does not need if (Meteor.isClient) - it alway is client. Similarly if (Meteor.isServer) code should be removed - it will never be executed. Code in server/ obeys similar rules: Meteor.isServer is always true and Meteor.isClient code can be removed.

A final, final comment (:wink:): you can paste your code between lines with triple backticks to get formatting and syntax highlighting:

```
paste code between backticks like this
```

#18

Well, I have encountered a similar problem with the last one. Your answer just solved my problem. Thank you!


#19

For some reason when I try to insert something into the collection it says:

insert failed: Access denied

Why?

Here’s my code and file structure:

Thank you!!!


#20

You’re not importing ‘Posts’ from the code you show below.

When you create the mongo collection, and give it the ‘Posts’ handle, you need to import that into your script for that template.

import { Posts } from './servermain.js' on the client in your template posts js file.