Object #<Object> has no method 'User' - javascript

everyone. I have a problem with my code and Meteor 0.9.4
Here is my code:
Server/publications.js
Meteor.publish('getUsers', function () {
var loggedInUser = Meteor.User();
if (Roles.userIsInRole(loggedInUser, ['admin'])) {
return Meteor.users.find({}, {fields: {
_id: 1,
emails: 1,
roles: 1
}});
}
this.stop();
return;
});
Lib/router.js
Router.map(function() {
this.route('dashboardUsers', {
layoutTemplate: 'dashboardLayout',
path: "/dashboard/users",
waitOn: function() {
return Meteor.subscribe('getUsers');
}
});
});
When I run meteor app, I have the following error:
=> App running at: http://localhost:3000/
I20141019-18:21:50.827(4)? Exception from sub 8CRiG3Jmdv4mohPhd TypeError: Object #<Object> has no method 'User'
I20141019-18:21:50.949(4)? at null._handler (app/server/publications.js:3:31)
I20141019-18:21:50.950(4)? at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1594)
I20141019-18:21:50.950(4)? at _.extend._runHandler (packages/ddp/livedata_server.js:943)
I20141019-18:21:50.950(4)? at _.extend._startSubscription (packages/ddp/livedata_server.js:769)
I20141019-18:21:50.951(4)? at _.extend.protocol_handlers.sub (packages/ddp/livedata_server.js:582)
I20141019-18:21:50.951(4)? at packages/ddp/livedata_server.js:546

If you have a look at the docs for Meteor.user, you'll see it works everywhere except publish functions. You need to do:
var loggedInUser = Meteor.users.findOne(this.userId);

The error your getting is because of the upper-case 'u' in Meteor.User it should be Meteor.user.
However, as David Weldon pointed out you'll need to use Meteor.users.findOne(this.userId) inside of publish functions.
For anyone else running into the error Object #<Object> has no method 'user' when trying to access Meteor.user() be sure you have the accounts-base package.

Related

Asteroid Oauth loginServiceConfiguration: 'google' of undefined

I've followed the 'naive' implementation in the project README: https://github.com/mondora/asteroid-oauth-mixin
The only difference in my code from the example is changing the arrow function to a traditional for the usage of this.
asteroid.ddp.on("added", ({collection, id, fields}: { collection: string; fields: {}, id: string }) => {
if (collection === "meteor_accounts_loginServiceConfiguration") {
asteroid.loginServiceConfiguration = {
...asteroid.loginServiceConfiguration,
[id]: {
_id: id,
...fields
}
};
}
});
});
asteroid.getServiceConfig = function(providerName: string) { // ts file
return this.loginServiceConfiguration[providerName];
}
When I do asteroid.loginWith('google')
index.ts:50 Uncaught TypeError: Cannot read property 'google' of undefined
On the meteor backend I also installed meteor add accounts-base accounts-google because I assume this is a dependency.
What am I missing? Thanks!
I've tried adding DDP.loginServiceConfiguration = {} before the snippet above which resolves the error but creates a new error.
asteroid-oauth-mixin.js:787 Uncaught TypeError: Cannot read property 'clientId' of undefined
at getOauthClientId (asteroid-oauth-mixin.js:787)
at Object.getOptions (asteroid-oauth-mixin.js:720)
at Asteroid.loginWith (asteroid-oauth-mixin.js:104)
at LoginForm../src/routes/accounts/auth/LoginForm.tsx.LoginForm.handleLoginWithGoogle (
Also when I run meteor mongo should db.meteor_accounts_loginServiceConfiguration.find().count() be 0 ?
I needed to meteor add service-configuration and setup my configure-accounts.js and create a google clientId for the application.
This gets me to the point where I have a popup and can choose which user to auth with. Then I receive a new error about target origin mismatch, but I'm going to close this question as resolved.

Meteor Router crashing - Cannot call method 'filters' of undefined

I have a page that I want the user to not be able to access unless they have a certain property. I have defined a filter as such:
Meteor.Router.filters({ //line 24
isX: function(page) {
if(Roles.userIsInRole(this.userId, ['X'])) {
return page;
} else {
return '/error';
}
}
});
Meteor.Router.filter(isX, {only : 'xPage'});
this code exists in my router.js file. When I try to compile it I get the following error:
\.meteor\packages\meteor-tool\1.1.4\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\fibers\future.js:245
throw(ex);
^
TypeError: Cannot call method 'filters' of undefined
at app\lib\router.js:24:15
at app\lib\router.js:70:3 //end of file
at \.meteor\local\build\programs\server\boot.js:222:10
at Array.forEach (native)
at Function._.each._.forEach (\.meteor\packages\meteor-tool\1.1.4\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\underscore\underscore.js:79:11)
at \.meteor\local\build\programs\server\boot.js:117:5
Exited with code: 8
Your application is crashing. Waiting for file change.
I'm at a loss because this is pretty much straight out of the examples for iron router.
I'm not sure where you are getting Meteor.Router.filters with regards to iron:router. You can do something very similar to what you need with an onBeforeAction though.
var isX = function() {
if (Roles.userIsInRole(this.userId, ["X"])) {
this.next();
}
else {
this.redirect("/error");
}
};
Router.onBeforeAction(isX, {
only: ["xPage"]
});
Router.route("/xPage", {
name: "xPage",
action: function() {
this.render("xPage");
}
});

Ember Route: Cannot read property ‘id’ of undefined

I'm getting this error, where I am trying to retrieve a user that DO NOT exist on purpose ofcouse:
Error while processing route: user Cannot read property 'id' of
undefined TypeError: Cannot read property 'id' of undefined
Instead of this console error, can I give the user an error message saying, 'the user does not exist' or something like that through the route or a controller to the view?
Heres my route
App.Router.map(function () {
this.resource('user', { path: '/user/:user_id' });
});
And here i retrieve the user
App.UserRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('user', params.user_id);
},
});
Hope you understand and can help me proceed. Thanks in regards!
So I'm not sure why missing fixtures data is giving you that particular error, but you can catch errors in routes using the error event. You can read about it here. You'll have to do something like this:
App.UserRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('user', params.user_id);
},
actions: {
error: function(error, transition) {
// Display some sort of message
alert("Sorry, we couldn't find that user.");
// Redirect to a different part of the application
this.transitionTo('index');
}
}
});

res.should.have.status gives me error

I'm new to mocha and should.js. I'm trying to check the response's status but it gives me TypeError: Object #<Assertion> has no method 'status' The code is like this:
describe('Local signup', function() {
it('should return error trying to save duplicate username', function(done) {
var profile = {
email: 'abcd#abcd.com',
password: 'Testing1234',
confirmPassword: 'Testing1234',
firstName: 'Abc',
lastName: 'Defg'
};
request(url)
.post('/user/signup')
.send(profile)
.end(function(err, res) {
if (err) {
throw err;
}
res.should.have.status(400);
done();
});
});
I also noticed that although I have declared var should = require('should'); , my ide notifies me that 'should' is a unused local variable. I don't really know why.
Try
res.status.should.be.equal(400);
or
res.should.have.property('status', 400);
And about " 'should' is a unused local variable". It's true. You don't use should directly. Only sideeffects. Try require('should'); instead.
Place the line:
require('should-http');
somewhere in your code. E.g.:
require('should-http');
describe('Test Something', function() {
...
As an addition to Yury answer. There is should-http package, which contain .status(code) assertion. You need to require somewhere it in code and it will be added to should.js.
I would switch to expect:
expect(res.status).to.be.eq(400);

Meteor.user().profile returns 'profile' undefined

I have a helper called 'isActive' and a template named 'create'.. see below
Template.create.isActive = function () {
return Meteor.user().profile.isActive;
};
When I try to run this code it returns the following in console: "Exception in template helper: TypeError: Cannot read property 'profile' of undefined".
Basically I want to pull the 'isActive' info from the current user profile and return it to the template. Any idea why this does not work?
Update
//startup on server side:
Meteor.publish("userData", function() {
if (this.userId) {
return Meteor.users.find({_id: this.userId},
{fields: {'profile.isActive': 1}});
} else {
this.ready();
}
});
//startup on client side
Meteor.subscribe('userData');
//router
this.route('list', {
path: 'list',
waitOn : function () {
return Meteor.subscribe('userData');
},
data : function () {
return Meteor.users.findOne({_id: this.params._id});
},
action : function () {
if (this.ready()) {
this.render();
}
}
});
Meteor.user() is undefined. You are either not logged in with any user or your template is rendered before the users collection is synced to the client. If you use a router like iron router you can wait for the collection being available or for the user being logged on.
Without using a router the easiest would be to check if the user is defined:
Template.create.isActive = function () {
return Meteor.user() && Meteor.user().profile.isActive;
}
Since Meteor.user() is reactive the template will be rerendered when the user changes, i.e. when it is available.
This is a common error btw, see also:
Meteor.user() Error: Uncaught TypeError: Cannot read property 'name' of null
How can I prevent Meteor.user()'s timing hole?

Categories

Resources