I have a meteor method defined on the client side using the pattern described here https://guide.meteor.com/methods.html#advanced-boilerplate
// files.collection.js
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
export const mediaFiles = new Mongo.Collection('media_files')
export const method_InsertFile = {
name: 'media_file.insert',
// Factor out validation so that it can be run independently (1)
validate(args) {
console.log('call validation: ', args)
new SimpleSchema({
title: { type: String },
filename: { type: String },
}).validate(args)
},
// Factor out Method body so that it can be called independently (3)
run({ filename, title }) {
let inserted_object = { title, filename }
mediaFiles.insert(inserted_object)
return inserted_object // object of interest
},
call(args, callback) {
console.log('call call method: ', args)
const options = {
returnStubValue: true, // (5)
throwStubExceptions: true // (6)
}
Meteor.apply(this.name, [args], options, callback);
}
};
if (Meteor.isServer) {
Meteor.methods({
// Actually register defined method with Meteor's DDP system
[method_InsertFile.name]: function (args) {
method_InsertFile.validate.call(this, args);
method_InsertFile.run.call(this, args);
},
});
}
The method is being called as shown below
import { method_InsertFile } from '../api/files.collection';
method_InsertFile.call(data, (err, res) => {
if (err) console.log("Return err ", err)
else {
console.log('result: ', res)
}
})
I want to retrieve the object inserted_object
at the end of the method call. I have tried returning a promise from within the call
block of the method definition like this
return new Promise((resolve, reject) => {
Meteor.apply(this.name, [args], options, (err, res) => {
resolve(res)
});
})
but result
returns undefined
.
Any pointers as to whether this is possible and how to accomplish it is appreciated.