Reputation: 1620
This code works when I move the insert after s3.putObject, but when I have it within the callback, it appears to work, but nothing is added to the database nor updated on the web page.
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
});
}
});
}
// });
}
});
Upvotes: 1
Views: 41
Reputation: 1620
I needed to bind to my environment because it everything must be executed within a fiber.
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'); }));
}
// });
}
});
Upvotes: 1