流星500错误

时间:2018-08-15 19:21:53

标签: reactjs meteor meteor-react

我正在开发一个以React为前端库的Meteor应用程序。 当前,我正在尝试将数据插入到Accounts集合中,但是我的代码出现500错误。不幸的是,我无法直接根据Meteor的经验使用控制台错误。

控制台错误:

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

我认为这可能与进口有关,但是我无法弄清楚这一点。

其余的代码:

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;

最后是我的订阅:

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({});
  });
});

附加:当我尝试解决此问题时,我还注意到我收到以下异常:

Exception while simulating the effect of invoking 'customers.insert' TypeError: "event is undefined"

@edit:修复了最后一个问题。做了一个小错字

1 个答案:

答案 0 :(得分:0)

控制台上的错误是参考错误,指出未定义文档。您如何插入新客户?您没有从代码中指定它。理想的方法是使用表单字段中的值创建客户对象,并在调用“ customer.insert”方法时将其传递给客户对象。        const客户= {          名称:     } //调用传入对象的方法     Meteor.call('customer.insert',客户) 在服务器端的“ customer.insert”方法上,在客户文档中进行插入。     Customer.insert(客户)