meteor-integration
meteor-integration copied to clipboard
userId with collection-hooks
Is there any possibility to have access in meteor methods to the userid of the account authenticated ? as in the following repo https://github.com/orionsoft/apollo-meteor/
I need it especially because i'm using that package matb33:collection-hooks
const Project = new Mongo.Collection('Project');
Project.before.insert(function(userId, doc) {
doc.createdAt = new Date();
doc.modifiedAt = new Date();
doc.owner = userId // Doesn't works
});
Thanks
Why not use Meteor.userId() for methods?
Don't know where collection-hooks gets the userId from, but if ^ works in methods, I would imagine collection-hooks should also work.
Just tried with Meteor.userId() and obtained the following error message :
Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
at AccountsServer.userId (packages/accounts-base/accounts_server.js:82:13)
at Object.Meteor.userId (packages/accounts-base/accounts_common.js:257:19)
at Object.<anonymous> (server/collections/projects/project.js:12:22)
at packages\matb33_collection-hooks.js:352:26
at Array.forEach (native)
at Function._.each._.forEach (packages\underscore.js:139:11)
at Object.<anonymous> (packages\matb33_collection-hooks.js:351:9)
at Object.collection.(anonymous function) [as insert] (packages\matb33_collection-hooks.js:146:21)
at [object Object].insert (packages/mongo/collection.js:521:37)
this.userId is undefined
Yeah, not sure why this package
https://github.com/matb33/meteor-collection-hooks
might be not accurately passing the userId arg to its hooks.
I know this ticket is almost a year old, but I like to mention this is available out of the box in https://github.com/Swydo/ddp-apollo, because it uses methods and publications under water 👍