【问题标题】:How to loop over rows after .fetchAll Bookshelf js + knex js?如何在.fetchAll Bookshelf js + knex js之后循环行?
【发布时间】:2016-02-27 15:01:44
【问题描述】:

我有一个 MySQL 数据库,我需要从 node.js 查询它

我正在为此使用书架和 knex。

我想获取表格的内容 - 我在我的 model.js 文件中定义了一个表格。我正在尝试这样的查询:

//select * from completedSentences;
Model.CompletedSentences.fetchAll().then(function (resData) {
        console.log(resData)
    })

我想知道如何遍历 resData,因为它应该是多行。

控制台的输出如下所示:我没有看到可以循环的行列表。我缺少什么?

CollectionBase {
  model:
   { [Function]
     NotFoundError: [Function: ErrorCtor],
     NoRowsUpdatedError: [Function: ErrorCtor],
     NoRowsDeletedError: [Function: ErrorCtor] },
  length: 1,
  models:
   [ ModelBase {
       attributes: [Object],
       _previousAttributes: [Object],
       changed: {},
       relations: {},
       cid: 'c4',
       id: 1 } ],
  _byId:
   { '1':
      ModelBase {
        attributes: [Object],
        _previousAttributes: [Object],
        changed: {},
        relations: {},
        cid: 'c4',
        id: 1 },
     c4:
      ModelBase {
        attributes: [Object],
        _previousAttributes: [Object],
        changed: {},
        relations: {},
        cid: 'c4',
        id: 1 } },
  _knex: null,
  _events: {},
  _eventsCount: 0 }

【问题讨论】:

    标签: javascript bookshelf.js knex.js


    【解决方案1】:

    答案很简单,试试这个 console.log(resData.toJSON())

    【讨论】:

      【解决方案2】:

      只需使用它的属性进行控制台即可。

      console.log(resData.attributes);
      

      你会得到结果对象虎钳。

      【讨论】:

        【解决方案3】:
        Model.CompletedSentences.fetchAll().then(function (resData) {
                console.log(resData.serialize())
            })
        

        输出为json格式

        http://bookshelfjs.org/#Model-instance-serialize

        【讨论】:

        • 这应该是公认的答案。简单直接。
        • 你是真正的 MVP @RalleSaid
        【解决方案4】:

        如果你不想使用 lodash,你可以这样做:

        new Model.CompletedSentences().fetchAll().then(function (resData) {
            resData.models.forEach( function (model) { 
                console.log(model.get('attribute');
                console.log(model.related('sth').get('attribute');
            })
        
        })
        

        【讨论】:

          【解决方案5】:

          Collection 类为此提供了一组 lodash 方法。

          你可以这样使用collection.forEach

          new Model.CompletedSentences().fetchAll().then(function (completedSentences) {
                  completedSentences.forEach(function (model) {
                      console.log(model.attributes)
                  })    
              })
          

          查看docsCollection 还有很多其他有用的方法。

          【讨论】:

          • 请注意,直接在 Collection 对象上使用 lodash 是行不通的(即,不要将它们视为数组并说_.forEach(collection, cb)。您必须调用集合上的方法:collection.forEach(cb),就像书架把它变成了_.forEach(collection.models, cb) 在你的封面下)。
          【解决方案6】:

          我找到了答案(文档非常神秘,希望这对其他人有帮助)

          new Model.CompletedSentences().fetchAll().then(function (resData) {
                  _.each(resData.models, function (model) { //I am looping over models using underscore, you can use any loop
                      console.log(model.attributes)
                  })
          
              })
          

          【讨论】:

          • 哇,我翻遍了整个文档,却找不到如何做这个简单的事情
          • 对我来说,我发现 result.lenght 不等于 result.models.length (((
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-24
          • 1970-01-01
          • 2017-12-19
          • 1970-01-01
          • 2020-03-15
          相关资源
          最近更新 更多