将多个值传递给流星验证方法



当我调用方法时,我想使用呼叫将多个值传递到该方法。我不确定为什么它只是错误的undfined。如果我只传递一个值profileCandidate,则方法有效。

路径:imports/api/profileCandidate/methods.js

export const insertProfileCandidate = new ValidatedMethod({
  name: 'profileCandidate.insert',
  validate: new SimpleSchema({
    firstName: { type: String },
    lat: { type: Number },
    lng: { type: Number },
  }).validator(),
  run({profileCandidate, lat, lng}) {
    ProfileCandidate.insert({
      userId: this.userId,
      createdAt: new Date(),
      name: {
        first: profileCandidate.firstName,
      },
      contact: {
        mobile: profileCandidate.mobile,
      },
      address: {
        lat: lat,
        lng: lng,
      }
    });
  },
});

路径:imports/ui/ProfileCandidateForm.jsx

var profileCandidate = this.state;
geocodeByAddress(this.state.address, (error, { lat, lng }) => {
  if (error) { return }
  if (!error) {
    insertProfileCandidate.call({profileCandidate, lat, lng}, (error) => {
      if (error) {
        alert(error.reason);
      }
    });
  }
}

验证函数中存在错误。尝试以下操作:

export const insertProfileCandidate = new ValidatedMethod({
  name: 'profileCandidate.insert',
  validate: new SimpleSchema({
  profileCandidate: { type: Object },
    'profileCandidate.firstName': { type: String },
    lat: { type: Number },
    lng: { type: Number },
  }).validator(),
  run({ profileCandidate, lat, lng }) {
    console.log(profileCandidate, lat, lng)
  },
});

您必须明确为profileCandidate对象中的每个字段定义一个验证器。

让我知道...

谢谢!

最新更新