I am working on a Meteor application with React as the frontend library. Currently, I am trying to insert data to the Accounts collection, but I get a 500 error on my code. Unfortunately, the console error is not something I can directly use with my experience with Meteor.
Error in the console:
I20180815-21:04:45.128(2)? Exception while invoking
method'customers.insert' ReferenceError: document is not defined
I20180815-21:04:45.129(2)? at MethodInvocation.customers.insert
(imports/collections/customers.js:13:27)
I20180815-21:04:45.129(2)? at maybeAuditArgumentChecks
(packages/ddp-server/livedata_server.js:1767:12)
I20180815-21:04:45.129(2)? at DDP._CurrentMethodInvocation.withValue
(packages/ddp-server/livedata_server.js:719:19)
I20180815-21:04:45.129(2)? at
Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1304:12)
I20180815-21:04:45.129(2)? at DDPServer._CurrentWriteFence.withValue
(packages/ddp-server/livedata_server.js:717:46)
I20180815-21:04:45.130(2)? at
Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1304:12)
I20180815-21:04:45.130(2)? at Promise (packages/ddp-server
/livedata_server.js:715:46)
I20180815-21:04:45.130(2)? at new Promise (<anonymous>)
I20180815-21:04:45.130(2)? at Session.method (packages/ddp-server.
/livedata_server.js:689:23)
I20180815-21:04:45.130(2)? at packages/ddp-server
/livedata_server.js:559:43
I think it might have something to do with the imports, but I cant figure this one out.
The rest of my code:
customer.js
import { Mongo } from 'meteor/mongo';
Meteor.methods({
'customers.insert': function() {
console.log('test');
},
'customers.remove': function(customer) {
return Customers.remove(customer);
}
});
export const Customers = new Mongo.Collection('customers');
customer_create.js
import React, { Component } from 'react';
class CustomersCreate extends Component {
onFormSubmit(event) {
event.preventDefault();
Meteor.call('customers.insert');
}
render() {
return (
<div>
<form onSubmit={this.onFormSubmit.bind(this)}>
<fieldset>
<label htmlFor="contactemail">E-mailadres contactpersoon</label>
<input id="contactemail" name="contactemail" type="text" />
</fieldset>
<fieldset>
<label htmlFor="password">Wachtwoord</label>
<input id="password" name="password" type="text" />
</fieldset>
<fieldset>
<label htmlFor="password">Wachtwoord bevestigen</label>
<input id="confirmpassword" name="confirmpassword" type="text" />
</fieldset>
<fieldset>
<button className="submit-button">Toevoegen</button>
</fieldset>
</form>
</div>
);
}
}
export default CustomersCreate;
And finally my subscription:
import { Meteor } from 'meteor/meteor';
import { RegistrationCards } from '../imports/collections/registrationcards';
import { Customers } from '../imports/collections/customers';
Meteor.startup(() => {
Meteor.publish('registrationcards', function() {
return RegistrationCards.find({});
});
Meteor.publish('customers', function() {
return Customers.find({});
});
});
addition: When I tried to fix this problem I also noticed that i get the following exception:
Exception while simulating the effect of invoking 'customers.insert' TypeError: "event is undefined"
@edit: Fixed the last issues. Made a small typo
The error on the console is a reference error which states that document is not defined. How are you inserting a new customer? You did not specify it from your code. The ideal way to go is create a customer object using values from the form fields and when you call the 'customer.insert' method;you pass it the customer object. const customer = { name: } //call the method passing in the object Meteor.call('customer.insert' , customer ) On the 'customer.insert' method on the server side you do an insert in the customer document. Customer.insert(customer)