[英]Simple-Schema for Meteor, method not returning a Number
I've seen lots of questions related to Meteor methods not being found but none of the solutions work for me. 我已经看到很多与流星方法有关的问题,但没有一种解决方案适合我。 I am trying to implement and auto-increment _id
field in my collection. 我正在尝试实现我的收藏_id
字段并使其自动递增。
Here are my files: 这是我的文件:
server/methods.js
Meteor.methods({
getNextSequence: function(counter_name) {
if (Counters.findOne({ _id: counter_name }) == 0) {
Counters.insert({
_id: counter_name,
seq: 0
});
console.log("Initialized counter: " + counter_name + " with a sequence value of 0.");
return 0;
}
Counters.update({
_id: counter_name
}, {
$inc: {
seq: 1
}
});
var ret = Counters.findOne({ _id: counter_name });
console.log(ret);
return ret.seq;
}
});
and lib/collections/simple-schemas.js
和lib/collections/simple-schemas.js
Schemas = {};
Schemas.Guests = new SimpleSchema({
_id: {
type: Number,
label: "ID",
min: 0,
autoValue: function() {
if (this.isInsert && Meteor.isClient) {
Meteor.call("getNextSequence", "guest_id", function(error, result) {
if (error) {
console.log("getNextSequence Error: " + error);
} else {
console.log(result);
return result;
}
});
}
// ELSE omitted intentionally
}
}
});
Guests.attachSchema(Schemas.Guests);
I'm getting an error that I'm assuming is coming from Simple-Schema and it's saying Error: ID must be a Number
, but my code is returning a number isn't it? 我遇到一个错误,我假设它是来自Simple-Schema的,它是说Error: ID must be a Number
,但是我的代码正在返回一个数字,不是吗? Also my console.log
messages are not showing up, the ones in the Meteor.methods call. 另外,我的console.log
消息没有显示,Meteor.methods调用中的消息。
Give _id
an optional: true
setting. 给_id
一个optional: true
设置。 The autoValue
will give it a real value but the required check is failing before it reaches the autoValue
. autoValue
会为它提供一个真实值,但是在达到autoValue
之前, 所需的检查失败。
The argument you have passed inside the Meteor.call is not a number, but a string "guest_id"
. 您在Meteor.call内部传递的参数不是数字,而是字符串"guest_id"
。 You should pass this.value
as it references the value passed to the key _id
. 您应该传递this.value
因为它引用传递给键_id
的值。
Your Meteor.call function would like the following: 您的Meteor.call函数需要以下内容:
Meteor.call("getNextSequence", this.value, function(error, result) {
if (error) {
console.log("getNextSequence Error: " + error);
} else {
console.log(result);
return result;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.