Webhooks before search result render filter:search.render



  • Hi NodeBB team,

    I am working on a project that requires some aggregation of the search results like following picture shows. I currently added a little customized hook with the controller search.js, but I would really like to have an official hook from the general release because though it doesn't affect usage right now it gives me a error of filter:search.render undefined.
    Screen Shot 2015-03-19 at 8.56.39 AM.png

    Following is my implementation and if you would like, I can also do a fork and pull request and implement the way according to your convention. Thanks!

            if (!plugins.hasListeners('filter:search.render')) {
    			res.render('search', results);
    		}
    		else{
    			plugins.fireHook('filter:search.render', results, function(results){
    				res.render('search', results);
    			});	
    		}

  • Admin

    @yeelan0319 I see no reason this hook cannot be added. Thanks for suggesting, feel free to go ahead and submit a pull request, and sign our new CAA


  • Admin

    There is a hook called filter:search.build is it not useful?



  • @baris Which version are you currently running? I am using v0.6.1, which I believe is the latest version?

    As far as I see, there is only one hook related to search which is filter:search.query. That hook run before the search is performed and is used by the dbsearch plugin. For my purpose, I want to perform some minor tweak to the results and render my own format, so if filter:search:build do exist, I think that fits my purpose.

    Thanks!😄


  • Admin



  • @baris That's great! I will go and pull the latest one.

    By the way, I wonder what is the reason for it triggering filter:search.render, undefined error? I didn't see anywhere in the module check the registration for the hooks and I wonder what I have missed. Just very curious about that. Thanks!

    By the way, your avatar looks soooooo cute, cannot staring at it☺


  • Admin

    @yeelan0319 Not sure post a stack trace if you have one.



  • @baris look at this:

    19/3 09:53 [37042] - error: [plugins] filter:search.render,   undefined
    

    That's the information I have got with no other output. I tried to isolate which line trigger it, it seems to be the line in the dbsearch plugin callback(results), which content is

    function(err ,v){
        memo = v;
        callback(err);
    }
    

    if there is no more wrapping's around the second callback, I guess it should trigger the callback function which was passed when the filter:search.render event is fired.


  • Admin

    I don't see a callback(results) in the db search plugin, can you post a link to that line of code?

    We usualy pass in an error object as the first param.



  • @baris Great hint acutally!

    I traced down to the hook.js and see how things got wired up.

    So it happens when I called plugins.fireHook('filter:search.render', params, callback), where callback as I posted in the first place only takes one parameter results.
    So in my registered plugin function summarizeCategories, I take exactly two parameter as following:

    function summarizeCategories(results, callback){
        //do something with the results
        callback(results);
    }
    

    Since I thought callback is the one I passed in!! 😱
    However, in hook.js, the callback function got wrapped around another callback function and the parameter that function takes are (err, values). Inside that the function, the original callback function was called, of course with with callback(err, results) but not callback(results).

    So in my registered function summarizeCategory I was passing in the result as err parameter and of course it gives me an err message. But then the falling call of original callback(err, results) still gives me the correct results information I needed in render. lol

    That's was have happened. It's kind of complicated to trace the error down since so many things named similar but it was great fun and I really enjoyed how thing get beautifully wired up in NodeBB. Great job guys!👏

    I wonder if there is any place that I can jot the point down so others may not fall in the same pitfall later? I saw your documentation is a bit out dated but you did mentioned it is going under a overhaul. I am looking forward to it.😆


 

| |