在回调中调用时插入不被调用,为什么会这样?

时间:2014-07-29 22:17:06

标签: javascript callback meteor

当我在s3.putObject之后移动插入时,此代码有效,但是当我在回调中使用它时,它似乎工作,但没有任何内容添加到数据库,也没有在网页上更新。

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


        }

    });

1 个答案:

答案 0 :(得分:1)

我需要绑定到我的环境,因为它必须在光纤内执行。

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

    }

        // });



}
});