训练营我使用knex /书架库教授postgres,但目前我还不知道如何使用reqular查询(即SELECT FROM等...)。 到目前为止,我还没有尝试过与knex / bookshelf库的关系查询,但我无法让它工作。我查看了文档,据我所知,我已经使用了他们所说的语法。
exports.getStudentsByClass = (key) => {
return Classroom.where({ id: key }).fetch({
withRelated: 'student'
})
.then(classroom => {
const students = classroom.related('student')
console.log(classroom.models.map(student => {
studentList.attributes
}))
return classroom
})
.catch(err => {
console.log(err)
})
}
这是我用来查询数据库的函数。当我控制台记录数据时,这就是我得到的
CollectionBase {
model:
{ [Function]
super_: [Function: Events],
extend: [Function: extend],
extended: [Function: extended],
__super__:
ModelBase {
_builder: [Function: builderFn],
_relation: [Function: _relation],
Collection: [Object] },
NotFoundError: [Function: ErrorCtor],
NoRowsUpdatedError: [Function: ErrorCtor],
NoRowsDeletedError: [Function: ErrorCtor],
forge: [Function: forge],
collection: [Function: collection],
count: [Function: count],
fetchAll: [Function: fetchAll],
where: [Function],
query: [Function] },
length: 5,
models:
[ ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c77',
id: 7 },
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c78',
id: 4 },
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c79',
id: 9 },
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c80',
id: 14 },
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c81',
id: 10 } ],
_byId:
{ '4':
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c78',
id: 4 },
'7':
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c77',
id: 7 },
'9':
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c79',
id: 9 },
'10':
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c81',
id: 10 },
'14':
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c80',
id: 14 },
c77:
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c77',
id: 7 },
c78:
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c78',
id: 4 },
c79:
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c79',
id: 9 },
c80:
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c80',
id: 14 },
c81:
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: {},
cid: 'c81',
id: 10 } },
relatedData:
RelationBase {
targetTableName: 'student',
targetIdAttribute: 'id',
type: 'hasMany',
target:
{ [Function]
super_: [Function: Events],
extend: [Function: extend],
extended: [Function: extended],
__super__: [Object],
NotFoundError: [Function: ErrorCtor],
NoRowsUpdatedError: [Function: ErrorCtor],
NoRowsDeletedError: [Function: ErrorCtor],
forge: [Function: forge],
collection: [Function: collection],
count: [Function: count],
fetchAll: [Function: fetchAll],
where: [Function],
query: [Function] },
foreignKey: 'classroom_id',
foreignKeyTarget: undefined,
parentId: 6,
parentTableName: 'classroom',
parentIdAttribute: 'id',
parentAttributes:
{ id: 6,
created_at: 2018-04-24T22:23:34.819Z,
updated_at: 2018-04-24T22:23:34.819Z,
name: ' Classroom 1' },
parentFk: 6 } }
所以我不确定如何使这项工作