meteor-integration icon indicating copy to clipboard operation
meteor-integration copied to clipboard

userId with collection-hooks

Open HBCharles opened this issue 8 years ago • 4 comments

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

HBCharles avatar Mar 26 '17 16:03 HBCharles

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.

lorensr avatar Mar 29 '17 15:03 lorensr

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

HBCharles avatar Apr 09 '17 13:04 HBCharles

Yeah, not sure why this package

https://github.com/matb33/meteor-collection-hooks

might be not accurately passing the userId arg to its hooks.

lorensr avatar Apr 19 '17 15:04 lorensr

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 👍

jamiter avatar Feb 26 '18 12:02 jamiter