How do you get documents that are outside of your initial publications? - meteor

For example, let's say I have posts that are locked with a password with the following fields (some may not have a password):
{
_id: String
password: String,
body: String,
createdAt: Date
}
The posts with a password only have some metadata published and the posts without a password is completely published:
Meteor.publish('locked_posts', function() {
return Posts.find({ password: { $exists: true } }, { fields: { createdAt: 1 } });
});
Meteor.publish('public_posts', function() {
return Posts.find({ password: { $exists: false } });
});
The view looks like this:
{{ #each posts }}
{{ #if password }}
<input type="password">
{{ else }}
<div>{{ body }}</div>
{{ /if }}
{{ /each }}
Inside a template, a user should be able to enter a post's password and get that post's body:
So do I re-publish that single post, for which the password was entered, with all the fields, and refresh the template to display this new post?

I would recommend you change this publish to include the password as an input parameter (maybe encrypted?).
Meteor.publish('locked_posts', function(id, password) {
check(id, String);
check(password, String);
return Posts.find({_id: id, password: password }, { fields: { createdAt: 1 } });
});
Then, you can call Meteor.subscribe('locked_posts', postId, somePassword) from your client code where you want the additional fields to be provided.

Related

Wordpress comment form - display errors on same page (woocommerce ratings)

On my single product page I have activated the ratings form.
Visitors are able to leave a rating which requires the following inputs:
1 to 5 stars
A message
E-Mail
Privacy policy checkbox
Captcha-Code
If any of those fields are missing I get redirected to the default error page from wordpress.
I already tried to create my own validation using jquery -> jquery.validate.js
Based on the information from the following question.
/* Comment form validation on same page */
function comment_validation_init() {
if (is_single() && comments_open()) {
?>
<script type="text/javascript" src="/js/jquery.validate.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
console.log("ready");
$('#commentform').validate({
rules: {
author: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
comment: {
required: true,
minlength: 20
}
},
messages: {
author: "Please enter your name",
email: "Please enter a valid email address.",
comment: "Please enter your comment"
},
errorElement: "div",
errorPlacement: function(error, element) {
element.after(error);
}
});
});
</script>
<?php
}
}
add_action('wp_footer', 'comment_validation_init');
After the form submission I still get redirected to the default wordpress error page. (wp-comments-post.php)
Is there any other way to prevent that from happening and display the errors within the form? (without changing the core files)
Since the form was loaded by Ajax the validation wasn´t working.
As a workaround I triggered my validation through the forms submit button.
$(document.body).on('click', '#commentform #submit', function(){
$("#commentform").validate({
rules: {
author: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
comment: {
required: true,
minlength: 20
}
},
messages: {
author: "Please enter your name",
email: "Please enter a valid email address.",
comment: "Please enter your comment"
},
errorElement: "div",
errorPlacement: function(error, element) {
element.after(error);
}
});
}
If someone got a "better" solution I would highly appreciate the information.

Meteor simple-todos app - How can I show incomplete task count only for current user?

I am working through this Meteor tutorial and I have updated imports/ui/task.html to only show the tasks to the user who created them as follows:
<template name="task">
{{#if isOwner}}
<li class="{{#if checked}}checked{{/if}} {{#if private}}private{{/if}}">
<button class="delete">×</button>
<input type="checkbox" checked="{{checked}}" class="toggle-checked" />
<span class="text">{{text}}</span>
</li>
{{/if}}
</template>
However, I still have the incomplete count showing tasks by all users and I want to change it to just the user who is logged in. This is the part of imports/ui/body.js that I think needs to be changed.
Template.body.helpers({
tasks() {
const instance = Template.instance();
if (instance.state.get('hideCompleted')) {
// If hide completed is checked, filter tasks
return Tasks.find({ checked: { $ne: true } }, { sort: { createdAt: -1 } });
}
// Otherwise, return all of the tasks
// Show newest tasks at the top. This is the meat of the thing!
return Tasks.find({}, { sort: { createdAt: -1 } });
},
incompleteCount() {
return Tasks.find({ checked: { $ne: true } }).count();
},
});
You only need to filter on ownerId which is used in this tutorial to associate a task with a user.
incompleteCount() {
return Tasks.find({ ownerId: Meteor.userId(), checked: { $ne: true } }).count();
},
Note that when you use multiple criteria like this they are implicitly ANDed.

How to update one field of a Meteor user profile without losing the others?

I have set several fields in my user profile. But I want to update one at a time. I do it like this :
Meteor.users.update(loggingInUserId, {
$set: {
profile: {
firstName
}
}
});
When I run this, the new value get updated but all the other fields are removed. Any idea how to do this right?
Example of a user :
{
_id: '11111',
profile: {
firstName: 'One',
lastName: 'Two',
contact: '34353'
}
}
Now when I run the above command, it will remove the lastName and the contact fields.
Try:
Meteor.users.update(loggingInUserId, {
$set: {
"profile.firstName": newValue
}
});

User email not showing in template [METEOR]!

I have a user index and would like to display information on each user. User ID shows up fine, but the app isn't showing emails.
Here is my template:
<template name="users">
<h1>List of all users</h1>
{{#each users}}
<div class="list_item">
<p>ID: {{_id}}</p>
<p>Email: {{email}}</p>
</div>
{{/each}}
</template>
And here are my routes:
Router.route('/users', function () {
this.render('users');
}, {
waitOn: function() {
return [
Meteor.subscribe('users')
]
},
data: {users: Meteor.users.find({})}
});
And finally, my publication:
Meteor.publish('users', function () {
return Meteor.users.find({}, {fields: {emails: 1, profile: 1}});
});
Any ideas?
The correct way to display the email would be :
<p>Email: {{emails.[0].address}}</p>
Email addresses are stored as an array in the user object.
You can check by typing Meteor.user() in the console :
Object {
...
emails: Array[1]
0: Object{
address: "username#domain.com",
verified: false
}
...
}
{{ currentUser.emails.[0].address }}

How to change a schema key?

I have a schema defined below and how can I change the predefined schema key (summary: key) via meteor template?
Schemas.Books = new SimpleSchema(
{
summary: {
type: String
}
}
);
For instance, I want to change this key through a session variable that was defined by router or through a user input.
Not Sure, Try this
If your schema is like this
Books = new SimpleSchema(
{
summary: {
type: String
}
}
);
then in tempalte helpers,
Books._schema.summary.type = function() {
return Session.get("typeValue");
};
In my project I have schema like this
RegisterSchema = new SimpleSchema({
name: {
type: String
},
email: {
type: String,
regEx: SimpleSchema.RegEx.Email
},
password: {
type: String,
label: "Password",
min: 8
},
confirmPassword: {
type: String,
label: "Confirm Password",
min: 8,
custom: function () {
if (this.value !== this.field('password').value) {
return "passwordMismatch";
}
}
}
});
and I'm dynamically setting optional value for email like
RegisterSchema._schema.email.optional = function() { return true };
this worked for me.
All d best
This is not the thing that I'm trying to do but I have learned a new trick : )
I want to change the schema key that I described above like this.
Books = new SimpleSchema(
{
bookName: {
type: String
}
}
);
Changing summary: with bookName:
Actually I want to define schema keys dynamically with respect to user information (userId, userName etc.).

Resources