Search code examples
apostrophe-cms

Working with cursors (NOT WORKING)


I've been following tutorial on Working With Cursors here. I wondered , where is the appropriate file code location modules for this cursor tutorial. Let's say I want to reuse this tutorial module on People Pieces . I have this code on people/index.js.

module.exports = {
    extend: 'apostrophe-pieces',
    permissionsFields : true,
    name: 'person',
    label: 'Person',
    pluralLabel: 'People',
    addFields : [
        {
            name: 'firstName',
            label: 'First Name',
            type: 'string',
            required: true,
        },
        {
            name: 'lastName',
            label: 'Last Name',
            type: 'string',
            required: true
        },
        {
            name: 'title',
            label: 'Full Name',
            type: 'string',
            required: true,
            contextual: true
        },
        {
            name: 'slug',
            label: 'Slug',
            type: 'string',
            required: true,
            contextual: true
        },
        {
            name: 'body',
            label: 'Biography',
            type: 'area',
            options: {
                widgets: {
                    'apostrophe-rich-text': {
                        toolbar: ['Bold', 'Italic', 'Link', 'Unlink']
                    },
                    'apostrophe-images': {}
                }
            }
        },
        {
            name: 'phone',
            label: 'Phone',
            type: 'string'
        },
        {
            name: 'thumbnail',
            label: 'Thumbnail',
            type: 'singleton',
            widgetType: 'apostrophe-images',
            options: {
                limit: 1,
                minSize: [200, 200],
                aspectRatio: [1, 1]
            }
        },
        {
            name: 'reputation',
            label: 'Reputation',
            type: 'integer'
        }
    ],
    arrangeFields: [{
            name: 'contact',
            label: 'Contact',
            fields: ['firstName', 'lastName', 'phone']
        },
        {
            name: 'admin',
            label: 'Administrative',
            fields: ['slug', 'published', 'tags']
        },
        {
            name: 'content',
            label: 'Biographical',
            fields: ['thumbnail', 'body']
        }
    ],
    construct: function(self, options) {
        self.beforeSave = function(req, piece, options, callback) {
            // Override title and MUST SET CONTEXTUAL to able to save. Let the 
            // backend self.beforeSave method do this thing.
            // You know why I don't set piece.slug ?
            // Because once you already set title , apostrophe made it for you :)
            // BUT must put contextual : true on slug. If not, it will prompt you :*
            piece.title = piece.firstName + ' ' + piece.lastName;
            return callback();
        }

        return self.apos.docs.getManager('person').find(req, {
                reputation: {
                    $gte: 30
                }
            }).sort({
                updatedAt: -1
            })
            .toArray(function (err, people) {
                console.log(people);
            });
    }
};

It Outputs the result on console like this : .find() is undefined

But then , i tried to use on browser side where self.pushAsset is involve, still does not console.log anything ! I know I should follow that tutorial using those schema. But let's brainstorming the code for a while for better understanding. How about if I reuse the code for the schema that I did on People Pieces ? Or is there any specific requirement or method to use for apos.getManager(piecesName) ? Oh btw, I did experimenting on people-widget/index.js too, but it did not workout with same error output. If this tutorial that I cannot follow , I will never did understand to use that code. I love ApostropheCMS <3


Solution

  • I found an answer for it. I also solve the req not defined on my terminal console.

    So I did this in app.js for cursor. Finally , it did output this stuff .

          'people-pages' : {
            extend : 'apostrophe-pieces-pages',
            construct: function (self, options) {
              return self.apos.docs.getManager('person').find({
                reputation: {
                  $gte: 30
                }
              }).toArray(function (err, people) {
                console.log("List of peoples \n",people);
              });
            }
          },