I am currently working on my own project, based on the discover meteor book.
I have subscriptions of my collection 'posts'.
I am using easy-search (a search package), and currently having some troubles.
I have easy search on a overlay called in with javascript.
When I search, it will always return the posts included in the subscriptions + search result as the result.
For example, if I'm in the post lists page, if I search for Chocolate, the result would be every posts in the post list page + chocolate keyword posts.
It goes the same for single post pages.
I was wondering if I could unsubscribe temporarily with a click event. In this case, would be the search button.
Don't use Meteor.publish for searching.
Create a Meteor.method on the server instead to find the search results.
Create a client-only (unmanaged) collection var results = new Mongo.Collection(null)
When you perform the search, remove all results results.remove({}) and then insert the results from the Meteor.method callback.
Then, to stop each search waiting until the next one completes (bad for autocomplete), you can look at calling the Meteor.method with wait: false eg.
Meteor.apply('mySearchMethod',[parameters], {wait: false}, function(err, res){});
To make this work, you need to call this.unblock() inside the search method.
Example Code
var searching = new ReactiveVar(false);
var currentSearch = "";
var results = new Mongo.Collection(null);
var search = function(searchText){
searchText = searchText.trim();
if (searchText === currentSearch){
// abort search if query wasn't different
return;
}
// clear results immediately (don't show expired results)
// NOTE: this can cause "flicker" as results are removed / re added
results.remove({});
if (searchText === ""){
return;
}
searching.set(true);
performSearch(searchText)
};
var performSearch = _.debounce(function(searchText){
currentSearch = searchText;
Meteor.apply('mySearchMethod', [searchText], {wait: false}, function(err, res){
if (err){
console.error(err);
}
if (currentSearrch !== searchText){
// query changed, results aren't relevant
return;
}
for (var i = 0; i < res.length; i++){
results.insert(res[i]);
}
searching.set(false);
})
}, 300);
Template.myTemplate.events({
'input #mySearchBox': function(e){
search($(e.currentTarget).val());
}
});
Template.myTemplate.heplers({
'searchResults': function(){
return results.find();
},
'showSpinner': function(){
return searching.get();
}
})
if (Meteor.isServer){
Meteor.methods({
'mySearchMethod': function(searchText){
check(searchText, String);
this.unblock();
var searchExp = new RegExp(RexExp.escape(searchText), 'i');
return myCollection.find({myField: searchExp}).fetch();
}
});
}
Related
Hello all I am trying to create an interactive Twitter bot that can fetch and post tweets at the users demand. Here is the code I have written thus far...
console.log("The bot is starting...");
var Twit = require('twit');
var config = require('./config')
var prompt = require('prompt');
prompt.start()
var T = new Twit(config);
console.log("Bot is ready to roll!");
var tweet_terms = "";
var tweet_count = 0;
var tweet_command = 0;
console.log("Choose a command...\n1. Get tweets \n2. Post tweet");
prompt.get(['command'], function(err, result) {
tweet_command = result.command
if (tweet_command == 1) {
console.log("You've chosen to get tweets.");
console.log("Enter in terms you want to search for seperated by commas, \
\nand also enter in the amount of tweets you want to receive back.");
prompt.get(['terms', 'count'], function(err, result) {
tweet_terms = result.terms;
tweet_count = result.count;
});
}
});
var params = {
q: tweet_terms,
count: tweet_count
}
T.get('search/tweets', params, gotData);
function gotData(err, data, response) {
var tweets = data.statuses;
for (var i = 0; i < tweets.length; i++) {
console.log(tweets[i].text);
}
}
I am trying to ask the user for input on what terms to search and how many tweets to gather. However my program is stopping before even the user input is prompted. Here is how the program is executing..
The bot is starting...
Bot is ready to roll!
Choose a command...
1. Get tweets
2. Post tweet
prompt: command: C:\Users\Kevin\Desktop\MERN Tutorials\Twit Twitter Bot\bot.js:42
for (var i = 0; i < tweets.length; i++) {
It looks like my gotData function is causing the issue but I don't understand exactly why my program is executing in this fashion.. My prompt isn't even allowing for user input.
TypeError: Cannot read property 'length' of undefined at gotData (C:\Users\X\Desktop\MERN Tutorials\Twit Twitter Bot\bot.js:42:31)
I do not understand why this function is even being called before the user input is handled.. I am new to NodeJS and am very confused why it is acting this way.
Any help would be much appreciated, thanks.
This line:
T.get('search/tweets', params, gotData);
is being called immediately after the application runs. After it's complete, you run a bunch of console.log() that appears to be providing the responses to the prompt. You wouldn't want to run this until after the user has input their choices (otherwise how could you know the params?).
Move the get call inside the callback of your last prompt:
prompt.get(['command'], function(err, result) {
tweet_command = result.command
if (tweet_command == 1) {
console.log("You've chosen to get tweets.");
console.log("Enter in terms you want to search for seperated by commas, \
\nand also enter in the amount of tweets you want to receive back.");
prompt.get(['terms', 'count'], function(err, result) {
tweet_terms = result.terms;
tweet_count = result.count;
T.get('search/tweets', params, gotData);
// ^ here!
});
} else {
// post a tweet code goes here
}
});
Now, while this works, it's not particularly flexible. You could probably rewrite this whole thing a little cleaner so that you can retrieve all the inputs from the user and then pass them as params to a single handler function.
I am trying to send a Push Notification through Parse Cloud Code when a certain object has been modified - "dirty"
I think I am almost there, but received an error because I believe am creating a new user instead of querying for one.
Parse.Cloud.beforeSave("Fact", function(request, response) {
var dirtyKeys = request.object.dirtyKeys();
for (var i = 0; i < dirtyKeys.length; ++i) {
var dirtyKey = dirtyKeys[i];
if (dirtyKey === "isValid") {
//send push
// Creates a pointer to _User with object id of userId
var targetUser = new Parse.User();
// targetUser.id = userId;
targetUser.id = request.object.userID;
var query = new Parse.Query(Parse.Installation);
query.equalTo('user', targetUser);
Parse.Push.send({
where: query,
data: {
alert: "Your Fact was approved :)"
}
});
return;
}
}
response.success();
});
I found this post related to my problem. My question now is how to integrate the user query in my beforeSave block. Ideally I would create another function for the user query and place that in my beforeSave block.
**5/14 Update
I took #toddg's advice and fixed the before save. Here is a clearer picture of what I am trying to do and the new error.
A couple points (as #Subash noted in the comments) before I get into the code:
Parse.Push.send is an async operation, so you'll want to ensure you call response.success() after your push send completes. I'm going to deal with this using Promises, as I think they are more flexible than callbacks. If you're not familiar, read about them here
The return in your if statement will likely prevent the response.success() from being called.
Here's my recommended way of doing it:
Parse.Cloud.beforeSave("Fact", function(request, response) {
// Keep track of whether we need to send the push notification
var shouldPushBeSent = false;
var dirtyKeys = request.object.dirtyKeys();
for (var i = 0; i < dirtyKeys.length; ++i) {
var dirtyKey = dirtyKeys[i];
if (dirtyKey === "isValid") {
shouldPushBeSent = true;
}
}
if (shouldPushBeSent) {
//send push
// Creates a pointer to _User with object id of userId
var targetUser = new Parse.User();
// targetUser.id = userId;
targetUser.id = request.object.userId;
var query = new Parse.Query(Parse.Installation);
// We want to pass the User object to the query rather than the UserId
query.equalTo('user', targetUser);
Parse.Push.send({
where: query, // Set our Installation query
data: {
alert: "Your fact was approved"
}
}).then(function(){
// Now we know the push notification was successfully sent
response.success();
}, function(error){
// There was an error sending the push notification
response.error("We had an error sending push: " + error);
});
} else {
// We don't need to send the push notification.
response.success();
}
});
By the way, I'm assuming that you have a column on your Installation class that tracks which user is associated with each Installation.
I have the class Friends on Parse:
Class Friends{
"from" : Pointer(_User)
"to" : Pointer(_User)
"allowSee" : Boolean
"block" : Boolean
"createdAt" : Date
"updatedAt" : Date
}
I have a Parse.Query where I get my friends, but this is Class Friends not class User. I try do a cicle for get the users in el field "from" but the array users return void
var user = Parse.User.current();
var me = {__type: 'Pointer',className: '_User', objectId: user.id}
var qFriends = new Parse.Query("Friends");
qFriends.equalTo("to", me);
qFriends.equalTo("allowSee", true);
qFriends.find().then(function(results){
for (i = 0; i < results.length; i++) {
var uFriendId = results[i].get("from").id;
var getUserById = new Parse.Query("_User");
getUserById.equalTo("objectId", uFriendId);
getUserById.first({
success: function(user) {
users.push(user);
},
error: function(error){
console.log(error);
}
})
}
return users;
}).then(function(friends){
response.success(friends);
}, function(error){
response.error();
});
Do exist way of get users by array of objectIds?, example:
var from =["f8dfg3","32fsg5s","43t4gsd"];
var arrUsers = getUser(from); // return array of PFUser with objectId in array from
Thanks,
Because your Friends class keeps pointers to users, there is a good way, in a single step, to eagerly fetch the related users. Before running the query, add:
qFriends.include("from");
qFriends.include("to");
Upon completion, the related users will be fetched, so you can get at them as follows:
qFriends.find().then(function(results){
for (i = 0; i < results.length; i++) {
var friend = results[i];
var fromUser = friend.get("from");
var toUser = friend.get("to");
// these will be fetched, so you can say things like:
console.log(fromUser.username);
// and so on
Incidentally, there's no need to build a pointer from the current user to qualify the query, the current user will do just fine as follows:
qFriends.equalTo("to", Parse.User.current());
Also, your goal should be covered with just the include() advice above, but at some point you might need to query the related objects in the results block the way your original post code attempts. The OP code makes two mistakes there: (1) you can just fetch(result.get("from")), the object -- no need to extract the objectId and call either get() or first() as you do. (2) all of those functions just named are asynchronous. The loop that launches them will terminate before any of them begin, so the line return users; will necessarily return nothing. The correct approach is to fill an array with the fetch()-returned promises and then return a new promise from Parse.Promise.when(fetchPromises);
I'm working on a search box for my site and want to build if from scratch (I know I probably shouldn't but it's a learning exercise as much as anything).
I am receiving a string from a text box and using the Jquery autocomplete function to display any results from Parse.
So far this works OK but when I type in a string that includes all the words in my database, but not in the correct order, I get no results.
e.g. if I search for "New article" it shows the article, whereas if I search for "article New" it doesn't.
I am retrieving the data with:
...
var searchString = document.getElementById("tags").value; // Get the search string
var str = searchString.split(" "); // Split it up into an array
var Articles = Parse.Object.extend("Articles");
var query = new Parse.Query(Articles);
query.containedIn("title", str); // This part doesn't work.
// query.contains("title", searchString); // I was using this and it works OK.
query.find({
...
EDIT:
Using the method suggested by bond below my cloud code is:
Parse.Cloud.define('searchArticles', function(request, response) {
console.log("About to request search terms"); // This never appears in cloud code logs
var input = request.params.searchTerms;
input = _.uniq(input);
input = _.filter(list, function(w) { return w.match(/^\w+$/); });
var searchQuery = new Parse.Query("Articles");
searchQuery.containedIn("titleArray", input);
searchQuery.find().then(function(articles) {
console.log("Success");
}, function(err) {
console.log("Failure");
});
});
and the function i'm using to call this is:
$("#searchBox").keyup(function() {
var availableArticles = [];
var searchString = document.getElementById("tags").value;
var str = searchString.split(" ");
Parse.Cloud.run('searchArticles', {"searchTerms":str}, {
success: function(articles) {
alert("Successful");
},
error: function(error) {
alert("Unsuccessful");
}
});
Note that none of the console.log's are ever written, and the alerts in the Parse.Cloud.run function never appear. The cloud code is running as it is displayed in Parse cloud code logs.
For search functions you may use a separate array column say keyWords. Where you save the keywords in title column in beforeSave function of Articles class. Then you may call separate search function to search while you type to search. Something like this:
Parse.Cloud.beforeSave("Articles", function(request, response) {
// set/update Articles keywords
}
Parse.Cloud.define("searchArticles", function(request, response) {
var input = request.params.query; //
// optimizing input
input = _.uniq(input);
input = _.filter(list, function(w) { return w.match(/^\w+$/); });
var searchQuery = new Parse.Query("AddressInfo");
searchQuery.containsAll("keyWords", input);
searchQuery.find().then(function(articles) {
// return success response
}, function(err) {
// handle error
});
}
You may call searchArticles from client. Running queries like containedIn in Cloud than on client would be faster to give your search results.
I'm a little confused about where to place a response.success() when using serial Promises.
Here's the situation: I've got a cloud function that accepts an array of email addresses and the current user. The function does the following:
Finds the current user based upon it's user object id.
Iterates over the array of emails addresses
Find if there is an existing user for each given email address
If there is an existing user, we check to see if the existing user and the current user are friends
If they are not friends, it creates a friendship.
Now when I run this function without a response.success(), it does exactly what I expect it to and the friendships entries are created. But no matter where I place the response in the code, I get the resulting response.success message and none of the serialized promises execute.
Why the resulting success/failure matters: I'm executing this function from an iOS app and I'd like to properly handle the success or failure cases correctly on the iOS side.
Here is the cloud function:
Parse.Cloud.define("friendExistingUsers", function(request, response) {
// Get our parameters
var addresses = request.params.emailAddresses;
var userId = request.params.user;
// Query for our user
var userQuery = new Parse.Query("User");
userQuery.equalTo("objectId", userId)
userQuery.first().then(function(currentUser) {
// if we find the user, walk the addresses
var promise = Parse.Promise.as("success");
_.each(addresses, function(address) {
console.log(address);
// add a then to our promise to handle whether a relationship is
// being created.
promise = promise.then(function() {
// find if there is a user for that address
var emailQuery = new Parse.Query("User");
emailQuery.equalTo("email", address);
emailQuery.first().then(function(addressUser) {
if (typeof addressUser != 'undefined') {
// found one.
console.log(addressUser);
// figure out if our current user and this user are
// friends.
var friendQuery = new Parse.Query("FVFriendship");
friendQuery.equalTo("from", currentUser);
friendQuery.equalTo("to", addressUser);
friendQuery.first().then(function(relationship) {
if (typeof relationship != 'undefined') {
// if they are, we need to pass.
console.log("Found a relationship: " = relationship)
} else {
// They are not. Add the friendship
var Friendship = Parse.Object.extend("FVFriendship");
var friendship = new Friendship();
friendship.set("from", currentUser);
friendship.set("to", addressUser);
friendship.save().then(function(result) {
console.log("Created a friendship: " + result)
});
};
});
} else {
// we did not find a user for that address
console.log("No user for " + address);
};
});
});
});
console.log(promise);
return promise;
}).then(function() {
response.success("success");
});
});
Thanks in Advance. Let me know if there's anything else I can add.
Your .then callback function attached to promise should return a promise. Missing this is a common mistake when using promises.
Also Parse doesn't seem to show objects with console.log as browsers do, so I wrap them into JSON.stringify().