流星:从客户端调用插入方法时"Cannot read property '_id' of undefined"



我会得到" typeError:当我尝试在客户端上使用流星方法将对象插入蒙哥集合中时,我无法读取未定义的属性'_id'。我正在使用Collection2核和经过验证的方法。尝试调用该方法的客户端代码是:

import { submitNewEvent } from '../../../api/events/events';
const newEvent = {
    eventName: "A String",
    eventDescription: "A String",
    eventPosition: {lat: 40, lng: -70},
};
submitNewEvent.validate(newEvent);
submitNewEvent.call(newEvent, (err, res) => {
    if (err) {
        console.log(err);
    } else {
        console.log(res);
    }
});

和方法/集合是在模块imports/api/events.js中定义的:

import { ValidatedMethod } from 'meteor/mdg:validated-method';
import eventSchema from './eventSchema';
Events = new Mongo.Collection('events');
Events.attachSchema(eventSchema);
export const submitNewEvent = new ValidatedMethod({
    name: 'submitNewEvent',
    validate: eventSchema.validator(),
    run({newEvent}) {
        Events.insert(newEvent);
    }
});

EventsChema本身并未指定_id字段,但所有其他字段都可以签出。奇怪的是,当我尝试将同一对象直接插入从启动服务器中的服务器中插入集合时,它可以正常工作,没有问题。什么可能导致该方法引起此问题?

整个错误文本是:

TypeError: Cannot read property '_id' of undefined
    at Mongo.Collection.doValidate (http://localhost:3000/packages/aldeed_collection2-core.js?hash=18cc61915c0e22cac3180c5c8c9e0ac91bdd188a:372:33)
    at Mongo.Collection.(anonymous function) [as insert] (http://localhost:3000/packages/aldeed_collection2-core.js?hash=18cc61915c0e22cac3180c5c8c9e0ac91bdd188a:259:25)
    at DDPCommon.MethodInvocation.run (http://localhost:3000/app/app.js?hash=b893ac14f5773e1f67641becf8b78b71756ca168:948:16)
    at ValidatedMethod._execute (http://localhost:3000/packages/mdg_validated-method.js?hash=0d0a63069b7327e1e04768f607f0db947dfe949d:130:45)
    at DDPCommon.MethodInvocation.ValidatedMethod.connection.methods._connection$methods.(anonymous function) (http://localhost:3000/packages/mdg_validated-method.js?hash=0d0a63069b7327e1e04768f607f0db947dfe949d:90:21)
    at http://localhost:3000/packages/ddp-client.js?hash=df770fd9a6a02fd730939b97d266ea2b12938e95:4088:25
    at Meteor.EnvironmentVariable.EVp.withValue (http://localhost:3000/packages/meteor.js?hash=6d285d84547b3dad9717a7c89c664b61b45ea3d8:1090:15)
    at Connection.apply (http://localhost:3000/packages/ddp-client.js?hash=df770fd9a6a02fd730939b97d266ea2b12938e95:4079:60)
    at ValidatedMethod.call (http://localhost:3000/packages/mdg_validated-method.js?hash=0d0a63069b7327e1e04768f607f0db947dfe949d:103:32)
    at MapComponent.newEventSubmitted (http://localhost:3000/app/app.js?hash=b893ac14f5773e1f67641becf8b78b71756ca168:415:28)

弄清楚答案是什么,这要归功于Buzz Moschetti的提示!该方法具有围绕newevent的角度括号,而newevent本身已经是一个对象,因此该文档在通往集合的路上丢失了。

相关内容

最新更新