javascript 如何在 .fetchAll Bookshelf js + knex js 之后循环遍历行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33906058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:01:59  来源:igfitidea点击:

How to loop over rows after .fetchAll Bookshelf js + knex js?

javascriptbookshelf.jsknex.js

提问by A.D

I have a MySQL Database which I need to query from node.js

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

I am using bookshelf and knex for this.

为此,我正在使用书架和膝关节。

I want to get the contents of a table - I have defined a table in my model.js file. I am attempting the query like this:

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

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

I would like to know how to loop over resData because it should be multiple rows.

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

The output of the console looks like this: I dont see a list of rows I can loop over.. What am i missing?

控制台的输出如下所示: 我没有看到可以循环的行列表.. 我错过了什么?

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 }

回答by A.D

I found the answer (the documentation is very cryptic, hope this helps others)

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

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)
        })

    })

回答by RalleSaid

Model.CompletedSentences.fetchAll().then(function (resData) {
        console.log(resData.serialize())
    })

output is in json format

输出为json格式

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

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

回答by pietrovismara

The Collectionclass has a set of lodash methods for this.

Collection班有一组这个lodash方法。

You can use collection.forEachthis way:

你可以这样使用collection.forEach

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

Check out the docs, there are many other useful methods for Collection.

查看文档,还有许多其他有用的方法Collection

回答by Salar

If you dont want to use lodash, you can do this:

如果你不想使用 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');
    })

})

回答by Chaudhary

simply just console with it attributes.

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

console.log(resData.attributes);

you will get results Objects vise.

你会得到结果对象虎钳。

回答by akshara

Answer is simple, try this console.log(resData.toJSON())

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