繁体   English   中英

流星的简单模式,方法不返回数字

[英]Simple-Schema for Meteor, method not returning a Number

我已经看到很多与流星方法有关的问题,但没有一种解决方案适合我。 我正在尝试实现我的收藏_id字段并使其自动递增。

这是我的文件:

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;
}

});

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);

我遇到一个错误,我假设它是来自Simple-Schema的,它是说Error: ID must be a Number ,但是我的代码正在返回一个数字,不是吗? 另外,我的console.log消息没有显示,Meteor.methods调用中的消息。

_id一个optional: true设置。 autoValue会为它提供一个真实值,但是在达到autoValue之前, 所需的检查失败。

您在Meteor.call内部传递的参数不是数字,而是字符串"guest_id" 您应该传递this.value因为它引用传递给键_id的值。

您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM