Meteor collection insert not updating on client - javascript

I'm currently having an issue with a collection insert in Meteor. I call a method to insert a new item into a collection. The server database shows the new item but the client side has no record of the collection. I've found if I refresh the page, my template referencing the collection populates and works.
Here is the method inserting the item located at 'lib/collections/items.js'
Items = new Mongo.Collection("items");
Meteor.methods({
addItem: function (text) {
if (! Meteor.userId()){
throw new Meteor.Error("not-authorized");
}
console.log(text);
console.log(Meteor.userId());
console.log(Meteor.user().username);
console.log(Meteor.user().household);
Items.insert({
text: text,
createdAt: new Date(), //current time
owner: Meteor.userId(),
username: Meteor.user().username,
household: Meteor.user().household
});
},
Here is the server publishing of the items located at '/server/main.js'
Meteor.publish("items", function(){
if(typeof Meteor.users.findOne({'_id': this.userId}) === 'undefined') return null;
return Items.find( {household : Meteor.users.findOne({'_id': this.userId}).household});
});
Here is the code calling the method located at 'client/main.js'
Template.body.events({
"submit .new-task": function (event) {
// This function is called when the new task form is submitted
var text = event.target.text.value;
text = text.trim();
if(text)//Check for non-null, non-empty
{
text = capitalizeEachWord(text);
console.log(text);
Meteor.call("addItem",text);
}
//Clear form
event.target.text.value = "";
//Prevent default form submit
return false;
},
Here is where the collection is queried from to display in a template at 'client/templates/itemList.js'
Template.itemList.helpers({
items: function() {
console.log('Trying to subscribe');
return Items.find({}, {sort : {checked: 1, createdAt: -1}});
}
I'm just starting to learn Meteor. I appreciate any help!
Edit
Thanks for the responses,
I've tried these suggestions and still getting the same result. I've tried a different way of relating users to households now and still having the same issues. I can insert an item from the client, it exist on the server side, and yet it doesn't exist on the client side. I'm sure i'm missing something small here but for the life of me, can't find it.
I know this is a huge dump of info but here is my updated source code (still producing the same problem) and logs. I've tried to put everything I know about this issue. Thanks for any help! I'm just starting out with Meteor and I know no one who develops in it so troubleshooting is a bit tough right now.
Client Specific Code
Located at 'client/main.js'
Template.body.helpers({
householdId: function() {
//return Meteor.users.findOne({'_id': Meteor.userId()}).household;
if(Meteor.userId() &&
Households.findOne({users : {$regex : new RegExp(Meteor.userId())}}))
{
console.log('Trying to get id');
return Households.findOne({users : {$regex : new RegExp(Meteor.userId())}})._id;
}
}
});
Template.body.events({
"submit .new-task": function (event) {
// This function is called when the new task form is submitted
var text = event.target.text.value;
text = text.trim();
if(text)//Check for non-null, non-empty
{
text = capitalizeEachWord(text);
console.log(text);
Meteor.call("addItem",text);
}
//Clear form
event.target.text.value = "";
//Prevent default form submit
return false;
},
"submit .new-household": function (event) {
// This function is called when the new task form is submitted
var insertedId;
var text = event.target.householdName.value;
text = text.trim();
if(text)//Check for non-null, non-empty
{
text = capitalizeEachWord(text);
Meteor.call("createHousehold", text);
}
//Prevent default form submit
return false;
}
});
Accounts.ui.config({
passwordSignupFields: "USERNAME_ONLY",
});
Code to subscribe to my collections located at 'client/application.js'
//Sub to our collections
Meteor.subscribe('households');
Meteor.subscribe('items');
Code iterating through the items to display located at 'client/templates/itemList.js'
Template.itemList.helpers({
items: function() {
console.log('Trying to subscribe');
return Items.find(
{
household : Households.findOne( {users : {$regex : new RegExp(this.userId)}})._id
}
, {sort : {checked: 1, createdAt: -1}});
},
householdId: function() {
//return Meteor.users.findOne({'_id': Meteor.userId()}).household;
if(Meteor.userId())
{
console.log('Trying to get id from ');
return Households.findOne({users : {$regex : new RegExp(Meteor.userId())}})._id;
}
}
});
Collection Code
Households collection code located at 'lib/collections/households.js'
Households = new Mongo.Collection("households");
Meteor.methods({
createHousehold: function(text) {
var insertedId;
insertedId = Households.insert({
householdName: text,
createdAt: new Date(), //current time
users: this.userId
});
}//Add user in the future
});
Items Collection code located at 'lib/collections/items.js'
Items = new Mongo.Collection("items");
Meteor.methods({
addItem: function (text) {
if (! Meteor.userId()){
throw new Meteor.Error("not-authorized");
}
console.log('Inserting item')
console.log('Item : ' + text);
console.log('UserId : ' + Meteor.userId());
console.log('Username : ' + Meteor.user().username);
console.log('Household : ' + Households.findOne( {users : {$regex : new RegExp(Meteor.userId())}})._id);
Items.insert({
text: text,
createdAt: new Date(), //current time
owner: Meteor.userId(),
username: Meteor.user().username,
household: Households.findOne( {users : {$regex : new RegExp(Meteor.userId())}})._id
});
return true;
},
deleteItem: function (itemId) {
Items.remove(itemId);
},
setChecked: function(itemId, setChecked) {
Items.update(itemId, { $set: {checked: setChecked}});
}
});
Server Code
The server code is located at 'server/main.js'
Meteor.publish("households", function(){
console.log('Trying to subscribe');
console.log(this.userId);
if(this.userId)
{
//var query = { users : new RegExp("/"+this.userId+"/")};
//console.log(Households.findOne( query ));
console.log(Households.findOne( {users : {$regex : new RegExp(this.userId)}}));
return Households.find( {users : {$regex : new RegExp(this.userId)}});
}
else
{
console.log('Too early');
return null;
}
});
Meteor.publish("items", function(){
console.log('Trying to get items');
if(!this.userId ||
!Households.findOne( {users : {$regex : new RegExp(this.userId)}}))
{
console.log('Returning null');
return null;
}
console.log(Items.findOne( {household : Households.findOne( {users : {$regex : new RegExp(this.userId)}}) }));
console.log(this.userId);
return Items.find( {household : Households.findOne( {users : {$regex : new RegExp(this.userId)}})._id });
});
Logs
If I start the site on localhost using meteor. When the page comes up (no user logged in), this the server console output:
=> App running at: http://localhost:3000/
I20141209-10:26:50.719(-5)? Trying to subscribe
I20141209-10:26:50.766(-5)? null
I20141209-10:26:50.766(-5)? Too early
I20141209-10:26:50.766(-5)? Trying to get items
I20141209-10:26:50.767(-5)? Returning null
Next I successfully create a user account (using accounts-ui/accounts-password packages). This is the output to that point.
I20141209-10:31:59.562(-5)? Trying to subscribe
I20141209-10:31:59.565(-5)? null
I20141209-10:31:59.566(-5)? Too early
I20141209-10:31:59.566(-5)? Trying to get items
I20141209-10:31:59.566(-5)? Returning null
I20141209-10:32:16.145(-5)? Trying to subscribe
I20141209-10:32:16.145(-5)? 8Skhof4jL2pSguT8Q
I20141209-10:32:16.146(-5)? undefined
I20141209-10:32:16.147(-5)? Trying to get items
I20141209-10:32:16.148(-5)? Returning null
Next I create a household using the .new-household form. No new output on the server at this point but here is the output on the client side:
main.js?ba2ac06f3ff7f471a7fa97093ac9ed5c01e0c8cd:7 Trying to get id
main.js?ba2ac06f3ff7f471a7fa97093ac9ed5c01e0c8cd:7 Trying to get id
itemList.js?a224bda493b90eb94bff9b88b48bb22eaa8aefe1:3 Trying to subscribe
main.js?ba2ac06f3ff7f471a7fa97093ac9ed5c01e0c8cd:7 Trying to get id
main.js?ba2ac06f3ff7f471a7fa97093ac9ed5c01e0c8cd:7 Trying to get id
itemList.js?a224bda493b90eb94bff9b88b48bb22eaa8aefe1:3 Trying to subscribe
At this point I add an item using the .new-task form. The task displays for a blink on screen then disappears. Here is the server output at this point:
I20141209-10:37:09.171(-5)? Inserting item
I20141209-10:37:09.172(-5)? Item : Beans
I20141209-10:37:09.172(-5)? UserId : 8Skhof4jL2pSguT8Q
I20141209-10:37:09.172(-5)? Username : pedro
I20141209-10:37:09.173(-5)? Household : M5NckT6ndqhRKCeWo
Here is the client output at this point:
main.js?ba2ac06f3ff7f471a7fa97093ac9ed5c01e0c8cd:21 Beans
items.js?2ca606d1e71e2e55d91421d37f060bd5d8db98fe:8 Inserting item
items.js?2ca606d1e71e2e55d91421d37f060bd5d8db98fe:9 Item : Beans
items.js?2ca606d1e71e2e55d91421d37f060bd5d8db98fe:10 UserId : 8Skhof4jL2pSguT8Q
items.js?2ca606d1e71e2e55d91421d37f060bd5d8db98fe:11 Username : pedro
items.js?2ca606d1e71e2e55d91421d37f060bd5d8db98fe:12 Household : M5NckT6ndqhRKCeWo
At this point the Item exists within the server database, but if I, using the web console in chrome, execute the command Items.findOne(); it returns undefined.

There could be more to it, but I couldn't see there where you have included the Meteor.subscribe("items") on the client side as that is what will pass the records to the client from the publish on the server.
Also in your publish, you can use
if (this.userId) {
//do stuff logged in
} else {
this.stop();
}
instead of selecting the Meteor.user() record for the user ID to detect if the client is logged in or not.

Your publication:
Meteor.publish("items", function(){
if(user = Meteor.user()){
return Items.find( {household : user.household});
}
else{
this.ready();
}
});
Your block helper:
Template.itemList.helpers({
items: function() {
return Items.find({}, {sort : {checked: 1, createdAt: -1}});
}
});
Your subscription (client side):
Meteor.subscribe("items");
If you are using iron:router. In the route you can define directly your helper and your subscription like this and just call {{items}} in your template:
Router.route('myRoute', function(){
this.render('myRouteTemplate');
}, {
path: '/myRoutePath',
waitOn: function(){
if(user = Meteor.user()) {
var subscriptions = [];
//
// Subscribe to your subscription
//===============================
subscriptions.push(Meteor.subscribe('items'));
return subscriptions;
}
},
data: function(){
var context;
if(this.ready()) {
if (user = Meteor.user()) {
context = {
items: Items.find( {household : user.household})
};
}
}
}
});
Always remember that when you publish a magazine, you need a subscriber process. Same thing for collections.
Cheers,

I ended up fixing this issue by implementing Iron Router in my application and waiting on subscriptions to be ready before rendering a template. I believe that my issue before was some kind of race condition but i'm not positive on the specifics of it. Either way i'm satisfied with my solution because it fixes my issue and structures the flow of my site better and more understandably I think.

Related

Why isn't my helper collection query not reflecting in the html template?

Following the tutorial on how to "Write an API", I seem to have gotten stuck and can't move beyond how to get the generated API Key to display in the template.
I have a query APIKeys.find().fetch() in the helper that should reflect correctly in the html template, but it doesn't. I have spent hours looking through my code and have failed to notice any errors in my code.
I am not that new to Meteor which makes it all the more annoying!
Kindly help!
Find below the template code:
/client/main.html
<template name="apiKey">
<div class="row">
<div class="col-xs-12 col-sm-6">
<h4 class="page-header">Your API Key</h4>
<p>To gain access to the Pizza API, use the following API Key.
Make sure to keep it super safe! <strong>If you'd like to
generate a new key, click the "refresh" icon on the field
below</strong>.</p>
<label class="sr-only" for="apiKey">Your API Key</label>
<div class="input-group">
<input type="text" readonly class="form-control" id="apiKey" placeholder="API Key" value="{{apiKey}}">
<div class="input-group-addon regenerate-api-key"><span class="glyphicon glyphicon-refresh"></span></div>
</div>
</div>
In the template above, NOTHING is rendered in the template under value="{{apiKey}}". I dont understand why this is.
Find below my helpers code: /client/main.js
import '../imports/api/tasks.js';
Template.apiKey.helpers({
apiKey: function() {
var apiKey = APIKeys.findOne();
if ( apiKey ) {
return apiKey.key;
console.log("Sucessful");
}
else {
console.log("Failed! Can't find: APIKeys.findOne()");
}
}
});
The helper code above renders this in the console: Failed! Can't find: APIKeys.findOne().
Further, when I query APIKeys.find().fetch() in the console, I get this:
[]
Find below my onCreated code: /client/main.js
Template.apiKey.onCreated(function(){
console.log("Your in onCreated!");
this.subscribe( "APIKey" );
});
The onCreated code above renders this in the console: Your in onCreated!.
Find below my events code that is triggered to generate a new API Key: /client/main.js
import '../imports/api/tasks.js';
Template.apiKey.events({
'click .regenerate-api-key': function( ){
var userId = Meteor.userId();
confirmRegeneration = confirm( "Are you sure? This will
invalidate your current key!" );
if ( confirmRegeneration ) {
Meteor.call( "regenerateApiKey", userId, function( error, response ) {
if ( error ) {
alert( error.reason, "danger" );
}
else {
alert( "All done! You have a new API key: " +response );
console.log("Response is: " +response);
}
});
}
}
});
The events code above renders a popup box with: All done! You have a new API key: 0. Also the console renders: Response is: 0.
Find below the regenerateApiKey method code /server/main.js
Meteor.methods({
regenerateApiKey: function( userId ){
check( userId, Meteor.userId() );
var newKey = Random.hexString( 32 );
console.log(">>>: " +newKey);
try {
var keyId = APIKeys.update( { "owner": userId }, {
$set: {
"key": newKey
}
});
console.log(">>> newKey : " +keyId);
return keyId;
} catch(exception) {
console.log("FAILED UPDATE")
return exception;
}
}
});
The method code above renders the below in the terminal:
>>>: af3233a999308e39f9471b790e121cf5
>>> newKey : 0
I have narrowed the issue in the code down to this. The keyId variable being equal to "0" suggests that the APIKeys collection isn't getting updated. Can anyone explain why this is happening?
I have included further information in hope that it will help.
Find below the code where i subscribe /client/main.js
Router.route('/apiKey', {
template: 'apiKey',
waitOn: function(){
return Meteor.subscribe('APIKey');
}
});
Find below the code where I publish /server/main.js
Meteor.publish( 'APIKey', function(){
var user = this.userId;
var data = APIKeys.find( { "owner": user }, {fields: { "key": 1 } } );
if ( data ) {
console.log("User: " +user+ " data is: " +data);
console.log("Was able to find data in Publish APIKey!");
console.log(APIKeys.find().fetch());
return data;
}
return this.ready();
});
The publish code above renders the below in the terminal:
User: xELzNtMQp7u9FpZib data is: [object Object]
Was able to find data in Publish APIKey!
[]
Find below the code where I declare the collection imports/api/tasks.js
import { Mongo } from "meteor/mongo";
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
global.APIKeys = new Meteor.Collection("apiKeys");
/*
* Allow
*/
APIKeys.allow({
insert: function(){
// Disallow inserts on the client by default.
return false;
},
update: function(){
// Disallow updates on the client by default.
return false;
},
remove: function(){
// Disallow removes on the client by default.
return false;
}
});
/*
* Deny
*/
APIKeys.deny({
insert: function(){
// Deny inserts on the client by default.
return true;
},
update: function(){
// Deny updates on the client by default.
return true;
},
remove: function(){
// Deny removes on the client by default.
return true;
}
});
The problem looks like you don't have any keys in the database, when you try to update it
Try swapping out APIKeys.update for APIKeys.upsert, which will create a key if one doesn't exist.
try {
var keyId = APIKeys.upsert( { "owner": userId }, {
$set: {
"key": newKey
}
});

Alerts/Notifications on Meteor application do not appear

I created a new "Alerts" collection. Its to show number of unread messages. Messages get submitted and appear, and theres no other error on console or server.
2nd issue is, when i click on the specific room, it is supposed to mark all new messages as "read". Somehow the number stays. Error shows Exception in queued task: .added#http://localhost:3000/app/lib/collections/messages.js
File structure:
roomList.js - shows a list of all rooms, shows number of unread messages
roomDetail.js - when click specific room in list, will mark message as
"read", unread number dissapears.
alerts.js (Alerts collection)
messages.js (Messages collection)
rooms.js (Rooms collection)
publications and sub js
Meteor.publish('alerts', function() {
return Alerts.find({ userId: this.userId, read: false });
});
Meteor.subscribe('alerts')
Alerts collection js
Alerts = new Mongo.Collection('alerts');
Alerts.allow({
update: ownsDocument,
//if removed, shows error:
// insert failed: Access denied. No allow validators set on restricted collection for method 'insert'.
insert: function(){
return true;
}
});
createMessageAlert = function(message) {
if ( message.user !== Meteor.userId() ){
Alerts.insert({
userId : message.user,
roomId : Router.current().params._id, //params id of current room
messageId : message._id,
read : false
});
}
};
roomDetail.js
Messages.insert({
roomId : Router.current().params._id,
msg : message,
user : Meteor.user()._id
});
template.find('input').value = '';
createMessageAlert(message);
roomsList.js
Template.list.helpers({
alerts: function (){
return Alerts.find({ userId: Meteor.userId(), read: false });
},
alertCount: function(){
return Alerts.find({ userId: Meteor.userId(), read: false }).count();
}
});
Template.allRooms.events({
'click a': function() { //click the a href to go into roomDetail
Alerts.update(this._id, {$set: {read: true}});
}
});
Ultimate solution :
You should call the createMessageAlert from a trigger when a new Message is added in Messages collection.
Pre-requisites:
create a trigger(MSG_OBSERVER) for Messages collection, where whenever anything is added to the collection, a createMessageAlert method is invoked provided with the added document object, so you can play inside the method and do desired operations.
When you are updating Alerts collection. The collection should be published in such a way(named as "null") that it should be reactive and should be available from all the instances accessing the same account from different browser instances.
Implementation
Just add below code in your collections.js
Meteor.method(
'createMessageAlert': function(id, fields) {
if ( fields.user !== Meteor.userId() ){
Alerts.insert({
userId : fields.user,
roomId : Router.current().params._id, //params id of current room
messageId : id,
read : false
});
}
}
);
var MSG_OBSERVER = Messages.find();
MSG_OBSERVER.observe({
added: function(id, fields){
Meteor.call('createMessageAlert', id, fields);
}
});
Meteor.publish(null ,function() { // null name means send to all clients
//use Messages.insert() to generate a fake message and add data in below params
Meteor.call('createMessageAlert', id, fields);
return Alerts.find();
});
Explaination
If you again read the pre-requisites, you will understand the code. Ensure you are subscribed with desired collection on client side. This code makes every collection involved and triggers very reactive and responsive.
Whatever you will add as messages will be added to Alerts as well.
Publishing "null" will simply publish data to all clients making UI behavior more robust and asynchronous.(I am using this feature in displaying real-time graphs, you don't even have to refresh UI and your data gets reflected.)
While publishing "null", you can create a fake Message OBJ and add it to call createMessageAlert function. You have to do this because you have to initiate publish on server restarts. choose Message Obj wisely so that it won't impact the work flow.

Collection helper throws a not defined error

Im trying to set up a list of "rooms". The intended sequence:
Click name of the user on his/her profile page
Check for existing room. If yes, go to that room, if not set up new room.
Im using both dburles:collection-helpers and reywood:publish-composite.
Its throwing me this error.
TypeError: Cannot read property 'username' of undefined
at Document.Rooms.helpers.recName (rooms.js:18)
And line 18 is:
return Meteor.users.findOne({ _id: this.receiver }).username;
i.e. _id: this.receiver is undefined.
I also tried to add protective checks in the collection helpers but error remains. I.e. return user && user.username for example.
One thing I note is that, I noticed when I click on the user, it goes to the room linked to the user's id. However when I click back, it jumps to a blank room with a different id thats unrecognised.
The relevant codes:
Server publish
Meteor.publish("onlusers", function (){
return Meteor.users.find({});
});
Rooms.js collection helper
Rooms.helpers({
recName: function() {
return Meteor.users.findOne({ _id: this.receiver }).username;
}
});
User.js (for profile page events)
Template.usersShow.events({
'click .user': function() {
var receiver = this._id;
Session.set('chatId', this._id);
var res = Rooms.findOne({
$or: [
{ owner : this._id },
{ receiver : this._id }
]
});
if(res){
Router.go('roomDetail', { "_id" : res._id });
} else {
var newRoom = Rooms.insert({
owner : Meteor.userId(),
receiver : receiver,
username : Meteor.user().username,
});
Session.set('roomid', newRoom);
Router.go('roomDetail', { "_id" : newRoom });
}
}
});
Your diagnosis:
_id: this.receiver is undefined.
May be misleading. What is also possible is that the user subscription isn't completely loaded when your helper runs. I was helping someone else with a similar problem with publish-composite the other day - the subscription is marked as ready when the parents are ready but the children may not have finished loading yet. I think of this as a bug in publish-composite, all the related objects really need to be there before the subscription can be marked as ready.
Instead of returning:
return Meteor.users.findOne({ _id: this.receiver }).username;
You can do:
var user = Meteor.users.findOne({ _id: this.receiver });
return user && user.username;
So you'll get nothing back until the user object loads but you won't throw an error.

How to prevent current user get notified?

I'm making an app that allows user to like and comment on other user post. I'm using Parse as my backend. I'm able to notified user everytime their post liked or commented. However if current user like or comment on their own post this current user still notified. How can I prevent this?
Here is the js code that I use:
Parse.Cloud.afterSave('Likes', function(request) {
// read pointer async
request.object.get("likedPost").fetch().then(function(like){
// 'post' is the commentedPost object here
var liker = like.get('createdBy');
// proceed with the rest of your code - unchanged
var query = new Parse.Query(Parse.Installation);
query.equalTo('jooveUser', liker);
Parse.Push.send({
where: query, // Set our Installation query.
data: {
alert: message = request.user.get('username') + ' liked your post',
badge: "Increment",
sound: "facebook_pop.mp3",
t : "l",
lid : request.object.id,
pid: request.object.get('likedPostId'),
lu : request.user.get('username'),
ca : request.object.createdAt,
pf : request.user.get('profilePicture')
}
}, {
success: function() {
console.log("push sent")
},
error: function(err) {
console.log("push not sent");
}
});
});
});
If I understand the context of where this code is correctly,
I recommend checking
if request.user.get("username") != Parse.CurrentUser.get("username")
Before sending out the push notification
Where is your cloud function being called from? If you're calling it from your ios code, then before you call the cloud code function, just prelude it with something like this:
if (PFUser.currentUser?.valueForKey("userName") as! String) != (parseUser.valueForKey("userName") as! String)

Meteor - rendering the name of the owner in a list objects

I'm getting an object not found error when I try and lookup the owner of the objects
i'm trying to render. I'm looping through a collection of video clips, that can be updated or administered by users. The code works fine when I'm logged in, but when I try to use this and I'm logged out, I get "Exception in queued task: TypeError: Cannot read property '_id' of undefined at Object.Template.video_info.creatorName "
I've tried to debug this by doing this:
console.log(this.owner);
var owner = Meteor.users.findOne(this.owner);
console.log(owner);
When I check the console log, I can see that the correct userid is being found, and when i manually run Meteor.users.findOne with this id I get a user object returned. Is there something strange about the timings in Meteor that is preventing this?
UPDATE: If I add a try...catch to the template creatorname function then 2 errors get logged but the template still renders... ??? Seems like this template is being called twice, one when it's not ready, and again once it is. Why would that be.
Example of the try...catch block:
Template.video_info.creatorName = function () {
try{
var owner = Meteor.users.findOne(this.owner);
if (owner._id === Meteor.userId())
return "me";
return displayName(owner);
} catch (e){
console.log(e);
}
};
ORIGINAL BROKEN CODE BELOW THIS POINT
This is in my HTML:
<body>
<div>
{{> video_list}}
</div>
</body>
<template name="video_list">
<h1>Video List</h1>
{{#each videos}}
<ul>
{{> video_info}}
</ul>
{{else}}
No videos yet.
{{/each}}
<div class="footer">
<button>Like!</button>
</div>
</template>
<template name="video_info">
<li class="video-list {{maybe_selected}}">
<img src="{{image}}" />
<div>
<h3>{{title}}</h3>
<p>{{description}}</p>
<h4>{{creatorName}}</h4>
</div>
</li>
</template>
This is in my client.js
Meteor.subscribe("videos");
if (Meteor.isClient) {
Template.video_list.videos = function() {
return Videos.find({}, {sort: {title: 1}});
};
Template.video_list.events = {
'click button': function(){
Videos.update(Session.get('session_video'),{$inc: {likes: 1}});
}
}
Template.video_info.maybe_selected = function() {
return Session.equals('session_video', this._id) ? "selected" : "";
}
Template.video_info.events = {
'click': function(){
Session.set('session_video', this._id);
}
}
Template.video_info.creatorName = function () {
var owner = Meteor.users.findOne(this.owner);
if (owner._id === Meteor.userId())
return "me";
return displayName(owner);
};
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
This is in my model.js
Videos = new Meteor.Collection("videos");
Videos.allow({
insert: function (userId, video) {
return false; // no cowboy inserts -- use createParty method
},
update: function (userId, video, fields, modifier) {
if (userId !== video.owner)
return false; // not the owner
var allowed = ["title", "description", "videoid", "image", "start"];
if (_.difference(fields, allowed).length)
return false; // tried to write to forbidden field
// A good improvement would be to validate the type of the new
// value of the field (and if a string, the length.) In the
// future Meteor will have a schema system to makes that easier.
return true;
},
remove: function (userId, video) {
// You can only remove parties that you created and nobody is going to.
return video.owner === userId; //&& attending(video) === 0;
}
});
var NonEmptyString = Match.Where(function (x) {
check(x, String);
return x.length !== 0;
});
var NonEmptyNumber = Match.Where(function (x) {
check(x, Number);
return x.length !== 0;
});
createVideo = function (options) {
var id = Random.id();
Meteor.call('createVideo', _.extend({ _id: id }, options));
return id;
};
Meteor.methods({
// options should include: title, description, x, y, public
createVideo: function (options) {
check(options, {
title: NonEmptyString,
description: NonEmptyString,
videoid: NonEmptyString,
image:NonEmptyString,
start: NonEmptyNumber,
_id: Match.Optional(NonEmptyString)
});
if (options.title.length > 100)
throw new Meteor.Error(413, "Title too long");
if (options.description.length > 1000)
throw new Meteor.Error(413, "Description too long");
if (! this.userId)
throw new Meteor.Error(403, "You must be logged in");
var id = options._id || Random.id();
Videos.insert({
_id: id,
owner: this.userId,
videoid: options.videoid,
image: options.image,
start: options.start,
title: options.title,
description: options.description,
public: !! options.public,
invited: [],
rsvps: []
});
return id;
},
});
///////////////////////////////////////////////////////////////////////////////
// Users
displayName = function (user) {
if (user.profile && user.profile.name)
return user.profile.name;
return user.emails[0].address;
};
var contactEmail = function (user) {
if (user.emails && user.emails.length)
return user.emails[0].address;
if (user.services && user.services.facebook && user.services.facebook.email)
return user.services.facebook.email;
return null;
};
I think I've found the solution to this one. After reading about the caching works in Meteor, I've discovered the subscription model and how this relates to meteors minimongo http://docs.meteor.com/#dataandsecurity. The reason this was failing then succeeding was that on the first load the data is still being cached in minimongo. I'm currently checking against Accounts login Services Configured to check if the user data has been loaded. I'm currently using this because I can't find a way to subscribe to the Metor users service, but my guess is that the Accounts login service would rely on the Metor users collection. My current solution looks like this:
if(Accounts.loginServicesConfigured()){
var owner = Meteor.users.findOne(this.owner);
if (owner._id === Meteor.userId())
return "me";
return displayName(owner);
}
Currently this appears to be working correctly. I'm still delving into how to subscribe to this users service.Couple of really userful resferences I found while searching for a solution for this
https://github.com/oortcloud/unofficial-meteor-faq
http://psychopyko.com/cool-stuff/meteor-6-simple-tips/
https://groups.google.com/forum/#!topic/meteor-talk/QKXe7qfBfqg
The app might not be publishing the user id to the client when you are logged out. You can try calling the find method on the server and return the user. Or use a different key for querying/

Categories

Resources