在回调中调用Insert时没有被调用,为什么会这样?

Insert not being called when called within a callback, why is this?

本文关键字:调用 为什么 回调 Insert      更新时间:2023-09-26

当我在s3之后移动插入时,此代码有效。但是当我在回调中拥有它时,它似乎可以工作,但没有添加到数据库中,也没有在网页上更新。

 Meteor.methods({
        createPost: function(options){
            // check(options,{
            var id = Random.id();
            var data = { Key: id, Body: 'Hello!'};
            if(Meteor.isServer){
            s3.putObject(data, function(err, data) {
                if (err) {
                    console.log("Error uploading data: ", err);
                } else {
                    console.log("Successfully uploaded data to s3.");
                    if(options.title.length > 200){
                        throw new Meteor.Error(413,"Title too long");
                    }
                    //if(!this.userId){
                        //throw new Meteor.Error(403, "You must be logged in");
                    //}
                    console.log("test")
                    var id = options._id || Random.id();
                    console.log(Posts.findOne());
                    Posts.insert({
                        _id: id,
                        owner: "test",
                        lat: options.lat,
                        lon: options.lon,
                        title: options.title,
                        public: !!options.public,
                        upvotes: 1,
                        downvotes: 0,
                        rank: 0 
                    });
                }
            });
        }
            // });

        }
    });

我需要绑定到我的环境,因为它必须在一个纤维中执行。

Meteor.methods({
    createPost: function(options){
        // check(options,{
        var id = Random.id();
        var data = { Key: id, Body: 'Hello!'};
        if(Meteor.isServer){
        s3.putObject(data, Meteor.bindEnvironment(function(err, data) {
            if (err) {
                console.log("Error uploading data: ", err);
            } else {
                console.log("Successfully uploaded data to s3.");
                if(options.title.length > 200){
                    throw new Meteor.Error(413,"Title too long");
                }
                //if(!this.userId){
                    //throw new Meteor.Error(403, "You must be logged in");
                //}
                console.log("test")
                var id = options._id || Random.id();
                console.log(Posts);
                 var ret = Posts.insert({
                    _id: id,
                    owner: "test",
                    lat: options.lat,
                    lon: options.lon,
                    title: options.title,
                    public: !!options.public,
                    upvotes: 1,
                    downvotes: 0,
                    rank: 0 
                }, function(error, results){
                    console.log(error)
                });
                 console.log("test2");
            }
        },function () { console.log('Failed to bind environment'); }));
    }
        // });

}
});