我是meteor的新手,我正在关注第一个教程(TODO LIST),但是使用了我自己的例子。 所以我能够通过客户端模板将一个表单提交到mongo DB中但不安全。现在,我正在使用此代码在客户端和服务器上定义文件的方法 - cliente.js:
import { Meteor } from 'meteor/meteor';
import {Mongo} from 'meteor/mongo';
import { check } from 'meteor/check';
export const Clientes = new Mongo.Collection('clientes');
Meteor.methods({
'clientes.insert'(nome, idade,genero,sessao,data,quemPreenche) {
check(nome, String);
check(idade, String);
check(genero, String);
check(sessao, String);
check(data, Date);
check(quemPreenche, String);
// Make sure the user is logged in before inserting a task
if (! this.userId) {
throw new Meteor.Error('not-authorized');
}
Clientes.insert({
nome,
idade,
genero,
sessao,
data,
quemPreenche,
createdAt: new Date(),
owner: this.userId,
username: Meteor.users.findOne(this.userId).username,
});
},
});
在客户端,我正在调用这样的方法 - novasessao.js:
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Perguntas } from '../api/perguntas.js';
import { Clientes } from '../api/clientes.js';
import './novasessao.html';
Template.novasessao.events({
'keypress form.nova-sessao' : function (event){
if (event.which === 13) {
event.preventDefault();
let dadosclientes = {};
const target = event.currentTarget;
const nome = target.nome.value;
const idade = target.idade.value;
const genero = target.genero.value;
const sessao = target.sessao.value;
const data = target.data.value;
const quemPreenche = target.quemPreenche.value;
Meteor.call('clientes.insert',{nome,idade,genero,sessao,data,quemPreenche});
}
}
});
问题是我无法再提交以下错误:
Exception while simulating the effect of invoking 'clientes.insert' errorClass {message: "Match error: Expected string, got object", path: "", sanitizedError: errorClass, errorType: "Match.Error", stack: "Error↵ at exports.check (http://localhost:3000/…983d07ae9423dd57f5b3c9f92b5593d2952f1b86:3717:25)"} Error
at exports.check (http://localhost:3000/packages/check.js?hash=63d7478b74cadc04d378bc2266ea8bd1bf6849d8:67:15)
at DDPCommon.MethodInvocation.clientesInsert (http://localhost:3000/app/app.js?hash=cfb80c1b0d41ad6244ac6e6510745a82024f58f6:200:7)
at http://localhost:3000/packages/ddp-client.js?hash=bc32a166cd269e06a394f9418e0024d805bab379:3973:25
at Meteor.EnvironmentVariable.withValue (http://localhost:3000/packages/meteor.js?hash=e3f53db3be730057fed1a5f709ecd5fc7cae1229:1077:17)
at Connection.apply (http://localhost:3000/packages/ddp-client.js?hash=bc32a166cd269e06a394f9418e0024d805bab379:3964:54)
at Connection.call (http://localhost:3000/packages/ddp-client.js?hash=bc32a166cd269e06a394f9418e0024d805bab379:3840:17)
at Object.keypressFormNovaSessao (http://localhost:3000/app/app.js?hash=cfb80c1b0d41ad6244ac6e6510745a82024f58f6:139:40)
at http://localhost:3000/packages/blaze.js?hash=983d07ae9423dd57f5b3c9f92b5593d2952f1b86:3718:20
at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?hash=983d07ae9423dd57f5b3c9f92b5593d2952f1b86:3687:12)
at Blaze.View.<anonymous> (http://localhost:3000/packages/blaze.js?hash=983d07ae9423dd57f5b3c9f92b5593d2952f1b86:3717:25)
meteor.js?hash=e3f53db…:930 Error invoking Method 'clientes.insert': Match failed [400]
有人可以帮忙吗?
谢谢!
答案 0 :(得分:0)
当你保存到Mongo时,你需要给它一个JSON对象。
替换部分客户端代码:
target: event.currentTarget;
let dadosclientes = {
nome: target.nome.value,
idade: target.idade.value,
genero: target.genero.value,
sessao: target.sessao.value,
data: target.data.value,
quemPreenche: target.quemPreenche.value
};
Meteor.call('clientes.insert',dadosclientes);
并在服务器中:
'clientes.insert'(dadosclientes) {
check(dadosclientes.nome, String);
check(dadosclientes.idade, String);
check(dadosclientes.genero, String);
check(dadosclientes.sessao, String);
check(dadosclientes.data, Date);
check(dadosclientes.quemPreenche, String);
// Make sure the user is logged in before inserting a task
if (! this.userId) {
throw new Meteor.Error('not-authorized');
}
dadosclientes.createdAt = new Date();
dadosclientes.owner = this.userId;
dadosclientes.username = Meteor.users.findOne(this.userId).username;
Clientes.insert(dadosclientes);
}
答案 1 :(得分:0)
您收到错误是因为您正在将对象类型值传递给方法。您还可以阅读错误&#34;匹配错误:预期字符串,获取对象&#34;。这是通过支票包发生的。
因此,在发送检查之前,请确保您发送的是正确的类型。
一旦通过所有检查。你的功能仍然会有mongo错误。因为您没有在insert方法中发送有效对象。
Clientes.insert({
nome, //This is a string
idade, //This is a string
genero, //This is a string
sessao, //This is a string
data, //This is a date
quemPreenche, //This is a string
createdAt: new Date(),
owner: this.userId,
username: Meteor.users.findOne(this.userId).username,
});
所以这不是一个有效的对象。所以你必须创建一个键值对象。喜欢:
Clientes.insert({
nome:nome,
idade:idade,
genero: genero,
sessao:sessao,
data:data,
quemPreenche:quemPreenche,
createdAt: new Date(),
owner: this.userId,
username: Meteor.users.findOne(this.userId).username,
});
答案 2 :(得分:0)
您的初始错误非常简单,您可以使用 6 个别参数的参数列表定义方法:
'clientes.insert'(nome,idade,genero,sessao,data,quemPreenche)
但是您使用以下命令从事件处理程序中调用它:
Meteor.call('clientes.insert',{nome,idade,genero,sessao,data,quemPreenche});
只有一个参数,一个对象。
删除调用中的大括号可以解决最初的问题:
Meteor.call('clientes.insert',nome,idade,genero,sessao,data,quemPreenche);