javascript – Sails.JS – 获取数据库中对象/行数的计数

前端之家收集整理的这篇文章主要介绍了javascript – Sails.JS – 获取数据库中对象/行数的计数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Sails.js中,通过生成模型和控制器为您完成了大量工作.控制器允许通过API访问数据.它允许通过传递开始/跳过/偏移和取/限制来轻松分页.但是为了让我确定一个集合的最大页面,我需要知道集合中有多少项.

我试图在生成器中扩展蓝图以公开新的API方法计数并创建计数操作.根据Waterline documentation,count是可以在模型上调用的动作.当我调用User.count()时,我得到了这个:

{ _context: 
   { identity: 'user',adapter: 
      { syncable: false,defaults: [Object],registerCollection: [Function],create: [Function],find: [Function],update: [Function],destroy: [Function],count: [Function],identity: 'odata',globalId: 'odata',config: [Object] },attributes: {},_cast: { _types: [Object] },_schema: { context: [Circular],schema: [Object],hasSchema: true },_validator: { validations: {} },_callbacks: 
      { beforeValidation: [Object],afterValidation: [Object],beforeUpdate: [Object],afterUpdate: [Object],beforeCreate: [Object],afterCreate: [Object],beforeDestroy: [Object],afterDestroy: [Object] },_instanceMethods: {},autoPK: true,autoCreatedAt: true,autoUpdatedAt: true,hasSchema: true,migrate: 'alter',_model: { [Function] extend: [Function],inject: [Function],__super__: {} },_transformer: { _transformations: {} },_tableName: 'user',_adapterDefs: [ [Object] ],_adapter: 
      { adapter: [Object],adapterDefs: [Object],query: [Circular],collection: 'user' },syncable: [Function],defaults: [Function],config: [Function],findOneById: [Function: dynamicMethod],findOneByIdIn: [Function: dynamicMethod],findOneByIdLike: [Function: dynamicMethod],findById: [Function: dynamicMethod],findByIdIn: [Function: dynamicMethod],findByIdLike: [Function: dynamicMethod],countById: [Function: dynamicMethod],countByIdIn: [Function: dynamicMethod],countByIdLike: [Function: dynamicMethod],idStartsWith: [Function: dynamicMethod],idContains: [Function: dynamicMethod],idEndsWith: [Function: dynamicMethod],findOneByCreatedAt: [Function: dynamicMethod],findOneByCreatedAtIn: [Function: dynamicMethod],findOneByCreatedAtLike: [Function: dynamicMethod],findByCreatedAt: [Function: dynamicMethod],findByCreatedAtIn: [Function: dynamicMethod],findByCreatedAtLike: [Function: dynamicMethod],countByCreatedAt: [Function: dynamicMethod],countByCreatedAtIn: [Function: dynamicMethod],countByCreatedAtLike: [Function: dynamicMethod],createdAtStartsWith: [Function: dynamicMethod],createdAtContains: [Function: dynamicMethod],createdAtEndsWith: [Function: dynamicMethod],findOneByUpdatedAt: [Function: dynamicMethod],findOneByUpdatedAtIn: [Function: dynamicMethod],findOneByUpdatedAtLike: [Function: dynamicMethod],findByUpdatedAt: [Function: dynamicMethod],findByUpdatedAtIn: [Function: dynamicMethod],findByUpdatedAtLike: [Function: dynamicMethod],countByUpdatedAt: [Function: dynamicMethod],countByUpdatedAtIn: [Function: dynamicMethod],countByUpdatedAtLike: [Function: dynamicMethod],updatedAtStartsWith: [Function: dynamicMethod],updatedAtContains: [Function: dynamicMethod],updatedAtEndsWith: [Function: dynamicMethod] },_method: [Function],_criteria: {},_values: null }

注意:我使用的是自定义适配器,但我已为适配器创建了count方法.

解决方法

获取用户集合中对象数量的计数:
User.count(function (err,num) {
    if(err) {
       return console.log(err);
    }
    console.log(num);
});

要将User.count包装在另一个方法中:

yourMethod = function (callback) {
    User.count(callback);
}

猜你在找的JavaScript相关文章