我有代码在裁剪图像之前将其保存到集合,但代码异步执行。在图像被裁剪之前插入到集合执行。流星执行功能同步
Meteor.methods({
'createWorkout': function(workoutFormContent, fileObj) {
// crop image to width:height = 3:2 aspect ratio
var workoutImage = gm(fileObj.path);
workoutImage.size(function(error, size) {
if (error) console.log(error);
height = size.height;
width = size.height * 1.5;
workoutImage
.gravity("Center")
.crop(width, height)
.write(fileObj.path, function(error) {
if (error) console.log(error)
});
});
// add image to form content and insert to collection
workoutFormContent.workoutImage = fileObj;
Workouts.insert(workoutFormContent, function(error) {
if (error) {
console.log(error);
}
});
},
});
如何能够同步运行此代码以便能够插入已裁剪的图像?
你需要在回调中运行它。 – SLaks