使用游标(不工作)

时间:2018-05-29 23:42:08

标签: apostrophe-cms

我一直在这里关注Working With Cursors的教程。我想知道,这个光标教程的适当文件代码位置模块在哪里。假设我想在People Pieces上重用此教程模块。我在 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);
            });
    }
};

它在控制台上输出结果如下: .find() is undefined

但是,然后,我试图在浏览器端使用self.pushAsset涉及,仍然没有console.log任何东西!我知道我应该使用这些模式来学习该教程。但是,为了更好地理解,让我们集思广益一段时间。如果我重用我在People Pieces上做的模式的代码怎么样?或者是否有任何特定的要求或方法用于apos.getManager(piecesName)?哦,顺便说一句,我也尝试过在people-widget / index.js上,但它没有使用相同的错误输出进行锻炼。如果我无法遵循本教程,我将永远不会理解使用该代码。我喜欢ApostropheCMS< 3

1 个答案:

答案 0 :(得分:0)

我找到了答案。我还解决了终端控制台上的req not defined问题。

所以我在 app.js 中为光标做了这个。最后,它确实输出了这些东西。

      '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);
          });
        }
      },