Search code examples
mongodbseneca

TypeError seneca indexof if not a function during respond


I have written a simple action which connects to mongo db using seneca-mongo store module, execute a list query and get the results. I can see that the query was successful and the correct results were fetched. When I try to send these results back to the client, the respond call errors out with following message and stack trace.

ERROR       act     root$           OUT             cmd:getparams,role:diff 11      {cmd:getparams,role:diff,payload:{id:scalaScan}}    ENTRY    (dqk22) -       seneca: Action cmd:getparams,role:diff callback threw: k.indexOf is not a function.     act_callback    {message:k.indexOf is not a function,pattern:cmd:getparams,role:diff,instance:Seneca/0.7.2/d0twcki9cmxg/1485517      TypeError: k.indexOf is not a function
    at /scratch/DiffAnalyzer/node_modules/seneca/node_modules/seneca-web/web.js:851:13
    at Function.forEach (/scratch/DiffAnalyzer/node_modules/lodash/dist/lodash.js:3298:15)
    at Object.defaultmodify [as modify] (/scratch/DiffAnalyzer/node_modules/seneca/node_modules/seneca-web/web.js:850:7)
    at respond (/scratch/DiffAnalyzer/node_modules/seneca/node_modules/seneca-web/web.js:654:22)
    at Seneca.<anonymous> (/scratch/DiffAnalyzer/node_modules/seneca/node_modules/seneca-web/web.js:401:7)
    at act_done (/scratch/DiffAnalyzer/node_modules/seneca/seneca.js:1554:16)
    at /scratch/DiffAnalyzer/node_modules/gate-executor/gate-executor.js:127:20
    at Seneca.<anonymous> (/scratch/DiffAnalyzer/analyze.js:613:5)
    at act_done (/scratch/DiffAnalyzer/node_modules/seneca/seneca.js:1554:16)
    at /scratch/DiffAnalyzer/node_modules/gate-executor/gate-executor.js:127:20
    at /scratch/DiffAnalyzer/node_modules/seneca-mongo-store/mongo-store.js:329:21
    at /scratch/DiffAnalyzer/node_modules/mongodb/lib/mongodb/cursor.js:271:33
    at /scratch/DiffAnalyzer/node_modules/mongodb/lib/mongodb/cursor.js:778:35
    at Cursor.close (/scratch/DiffAnalyzer/node_modules/mongodb/lib/mongodb/cursor.js:1009:5)
    at Cursor.nextObject (/scratch/DiffAnalyzer/node_modules/mongodb/lib/mongodb/cursor.js:778:17)
    at Cursor.each (/scratch/DiffAnalyzer/node_modules/mongodb/lib/mongodb/cursor.js:264:12)

The action that I have written is

seneca.add("role:diff,cmd:getparams", function(msg, respond) {
    seneca.ready(function() {
        var collection = seneca.make$("paramStore");
        var f = msg.payload;

        seneca.log.info("Filter", f);
        collection.list$(f, function(err, ob) {
            if (err) {
                seneca.log.error(err);
                respond(err);
            } else {
                seneca.log.info("Result", ob);
                respond(null, ob);
            }
        });
    });
});

The same piece of code was working and now I am getting this error. Not sure what changed. Any help/suggestions are greatly appreciated.


Solution

  • The issue I was facing was because of this bit of code in the module's js file

    if( _.isObject( result.out ) ) {
        _.each(result.out,function(v,k){
          if(~k.indexOf('$') && 'http$' !== k) {
            delete result.out[k]
          }
        })
    

    The _.each function is meant to parse a JSON object, where in my case the out was actually a JSON array. Wrapping the array into an object resolved it.