Hi, Martin:

I suspect the error is being thrown from your code from the 
document.content.name path.

When you snippet, the first document is the search report, which does not have 
a content key.

Only the actual document descriptors have the content key.  (As noted 
previously, you can suppress the actual documents if you only want the report 
by specifying the categories:'none' option.)

By the way the best practice with Promises is to supply a catch callback, as in:

  db.documents.query(...)
    .then(function(successResponse){...})
    .catch(function(failureError){...});

An error in your success callback will not call an error callback in the same 
.then() clause but will call a subsequent error callback.  That's why it's a 
good idea to use the catch callback.

The only reason to use an error callback in a .then() clause is to provide 
special handling for an error upstream of a catch callback.

The try/catch statement will only catch errors in the code that sets up the 
promises and not errors when the callbacks are executed asynchronously.  (In 
addition, try/catch statements appear to cause the v8 JavaScript engine to 
deoptimize the function at present.)

It's easy to confuse the try/catch statement with the catch callback, but 
that's part of the Promises standard.


Hoping that helps,


Erik Hennum

________________________________
From: [email protected] 
[[email protected]] on behalf of Martin Vollmix 
[[email protected]]
Sent: Sunday, June 07, 2015 11:23 AM
To: 'MarkLogic Developer Discussion'
Subject: Re: [MarkLogic Dev General] snippet do not work in querybuilder node.js

For better documentation I send you my complete POST Request Handler

router.post('/search', function (req, res) {
    try {
        db.documents.query(
            qb.where(
                qb.parsedFrom(req.body.SearchString)
            ).slice(qb.snippet())
        //.withOptions({categories: ['content', 'metadata']})
        //.withOptions({categories: ['content']})

        )
    .result(function (documents) {
            documents.forEach(function (document) {
                console.log(document.content.name + " at " + document.uri);
            })
            res.render('index', { title: 'MVSearch', results : documents });
            res.end();
        }, function (error) {
            console.dir(error);
            res.render('index', { title: 'MVSearch', error : error });
            res.end();
        });
    }
    catch (e) {
        res.render('index', { title: 'MVSearch', session : req.session, exp : e 
});
        console.log(e);
    }
});

As soon as I use the slice I got the already mentioned error

Best Regards

Martin

Von: [email protected] 
[mailto:[email protected]] Im Auftrag von Martin Vollmix
Gesendet: Sonntag, 7. Juni 2015 17:24
An: 'MarkLogic Developer Discussion'
Betreff: [MarkLogic Dev General] snippet do not work in querybuilder node.js

Hi,

the next problem I have is that snippet does not work in my node.js app

my query looks like this:

    db.documents.query(
          qb.where(qb.parsedFrom(req.body.SearchString))
               .slice(qb.snippet())
            //.withOptions({categories: ['content', 'metadata']})
            //.withOptions({categories: ['content']})
    )

The error I get is this one:

E:\Git\ExpressTest\node_modules\marklogic\node_modules\bluebird\js\main\async.js
:43
        fn = function () { throw arg; };
                                 ^
TypeError: Cannot read property 'name' of undefined
    at E:\Git\ExpressTest\routes\index.js:40:41
    at Array.forEach (native)
    at E:\Git\ExpressTest\routes\index.js:39:19
    at tryCatcher (E:\Git\ExpressTest\node_modules\marklogic\node_modules\bluebi
rd\js\main\util.js:24:31)
    at Promise.module.exports.Promise._settlePromiseFromHandler (E:\Git\ExpressT
est\node_modules\marklogic\node_modules\bluebird\js\main\promise.js:454:31)
    at Promise.module.exports.Promise._settlePromiseAt (E:\Git\ExpressTest\node_
modules\marklogic\node_modules\bluebird\js\main\promise.js:530:18)
    at Promise.module.exports.Promise._settlePromises (E:\Git\ExpressTest\node_m
odules\marklogic\node_modules\bluebird\js\main\promise.js:646:14)
    at Async._drainQueue (E:\Git\ExpressTest\node_modules\marklogic\node_modules
\bluebird\js\main\async.js:177:16)
    at Async._drainQueues (E:\Git\ExpressTest\node_modules\marklogic\node_module
s\bluebird\js\main\async.js:187:10)
    at Immediate.drainQueues [as _onImmediate] (E:\Git\ExpressTest\node_modules\
marklogic\node_modules\bluebird\js\main\async.js:15:14)
    at processImmediate [as _immediateCallback] (timers.js:358:17)

What am I doing wrong?

Any hints

Best Regards

Martin

_______________________________________________
General mailing list
[email protected]
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to