Javascript 如何获取按限额返回的项目索引&;降序createAt

Javascript 如何获取按限额返回的项目索引&;降序createAt,javascript,express,parse-platform,Javascript,Express,Parse Platform,这是我第一次使用Parse,到目前为止我很喜欢它,读写数据都很有效 我现在想做一些看似简单的事情,但在解析的上下文中似乎很难实现,我的谷歌fu让我失望 当我找到这样的项目时: var query = new Parse.Query(CommentClass); query.limit(2); query.descending('createdAt'); query.find({ success: function(object) { callback({

这是我第一次使用Parse,到目前为止我很喜欢它,读写数据都很有效

我现在想做一些看似简单的事情,但在解析的上下文中似乎很难实现,我的谷歌fu让我失望

当我找到这样的项目时:

var query = new Parse.Query(CommentClass);
query.limit(2);
query.descending('createdAt');
query.find({
    success: function(object) {
        callback({
            comments: object
        });
    },
    error: function(object, error) {
        console.log('there was an error');
    }
});

我想知道总列表中返回项目的索引是什么。如果我的列表有5个项目,这将返回最后创建的2个项目,但是如果没有另一个请求知道列表中项目的数量,这是不可能的。

在解析中没有简单的方法。一种方法是将全局索引保存在单独的对象中。在每个新的注释中,您需要获得这个全局索引,增加它,并将其放入注释中。但如果删除评论,它可能会变得一团糟。下面是一个假设不删除注释的示例:

SequenceForComment.js

// A class called SequenceForComment. Only one row. 
// Only one integer property called 'sequence'.
// You can create the row by using the Parse dashboard in the beginning. 

var SequenceForComment = Parse.Object.extend("SequenceForComment");

function getSequenceForComment(callback) {
  var query = new Parse.Query(SequenceForComment);
  return query.first().then(function (object) {
    // https://parse.com/docs/js/api/classes/Parse.Object.html#methods_increment
    //Increment is atomic.
    object.increment('sequence');
    return object.save();
  }).then(function (object) {
    callback(object.get('sequence'));
  }, function (error) {
    console.log(error);
    callback(undefined);
  });
}

module.exports = {
  getSequenceForComment: getSequenceForComment
};
var SequenceModule = require("cloud/SequenceForComment.js");

Parse.Cloud.beforeSave("Comment", function(request, response) {
  var comment = request.object;

  // First time this comment will be saved.
  // https://parse.com/docs/js/api/classes/Parse.Object.html#methods_isNew
  if (comment.isNew()) {

    // Get a sequence/index for the new comment
    SequenceModule.getSequenceForComment(function(sequence) {
      if (sequence) {
        comment.set("sequence", sequence);
        response.success();
      } else {
        response.error('Could not get a sequence.');
      }
    });
  } else { // Not a new save, already has an index
    response.success();
  }
});
main.js

// A class called SequenceForComment. Only one row. 
// Only one integer property called 'sequence'.
// You can create the row by using the Parse dashboard in the beginning. 

var SequenceForComment = Parse.Object.extend("SequenceForComment");

function getSequenceForComment(callback) {
  var query = new Parse.Query(SequenceForComment);
  return query.first().then(function (object) {
    // https://parse.com/docs/js/api/classes/Parse.Object.html#methods_increment
    //Increment is atomic.
    object.increment('sequence');
    return object.save();
  }).then(function (object) {
    callback(object.get('sequence'));
  }, function (error) {
    console.log(error);
    callback(undefined);
  });
}

module.exports = {
  getSequenceForComment: getSequenceForComment
};
var SequenceModule = require("cloud/SequenceForComment.js");

Parse.Cloud.beforeSave("Comment", function(request, response) {
  var comment = request.object;

  // First time this comment will be saved.
  // https://parse.com/docs/js/api/classes/Parse.Object.html#methods_isNew
  if (comment.isNew()) {

    // Get a sequence/index for the new comment
    SequenceModule.getSequenceForComment(function(sequence) {
      if (sequence) {
        comment.set("sequence", sequence);
        response.success();
      } else {
        response.error('Could not get a sequence.');
      }
    });
  } else { // Not a new save, already has an index
    response.success();
  }
});

好的,我会添加一些片段,但是我应该去哪里尝试解决这个问题呢?文档建议了一种方法,但没有进一步解释。索引应该是什么?不仅仅是结果中的项目顺序?它是“列表中的索引”还是“类中的索引(创建)”?列表中的索引,如果我在列表中有4个项目,当我添加一个新项目时,我希望它的索引为4/5,这取决于它是否基于0。您想要的与列表本身给您的行号之间有什么区别?