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.
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.
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?
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.
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.
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 (): you can paste your code between lines with triple backticks to get formatting and syntax highlighting: