我正在使用其订阅产品获得所有与姓名相同的成员(例如'Malith)。该功能正在100%工作。同样,使用该输出,我尝试返回总记录,以便前端可以轻松设置分页,限制和偏移量。但是我正在克隆不是一个功能。
下面是我的代码
static async listMembers(params, options) {
assert.string(params.query, 'params.query')
assert.object(options, 'options')
assert.func(options.db, 'options.db')
const { db } = options
const { query } = params
const matchingUsersQuery = await Common.paginateWith(params)(
User
.withFullNameOrEmailMatching(query, options)
.orderBy([
{ column: 'users.full_name', order: 'desc' },
{ column: 'users.id', order: 'desc' }
])
)
const users = await matchingUsersQuery
const usersIds = users.map(user => user.id)
const picDataAggregate = await db.select('user_id', 'subscription_id')
.from('person_in_charge')
.whereIn('user_id', usersIds)
const subscriptionProductsDataAggregate = await db.select(db.raw('sp.*'), 'spu.user_id')
.from('subscription_products AS sp')
.innerJoin(
db('subscription_product_users')
.whereIn('user_id', usersIds)
.as('spu'),
'spu.subscription_product_id', 'sp.id'
)
const subscriptionsIds = [
...new Set([
...picDataAggregate.map(pic => pic.subscription_id),
...subscriptionProductsDataAggregate.map(sp => sp.subscription_id)
])
]
const subscriptionsDataAggregate = await db.select(
'spu.user_id',
'fu.id AS facility_unit_id',
'fu.name AS facility_unit_name',
'prod.id AS product_id',
'prod.name AS product_name',
'subs.id AS subscription_id',
'subs.status AS subscription_status',
'subs.agreement_no AS subscription_agreement_no',
'subs.type AS subscription_type',
'subs.start_date AS subscription_start_date',
'subs.end_date AS subscription_end_date',
'props.name AS property_name'
)
.from(
db('subscriptions_v2')
.whereIn('id', subscriptionsIds)
.as('subs')
)
.leftJoin('subscription_products AS sp', 'sp.subscription_id', 'subs.id')
.leftJoin(
db('subscription_product_users AS spu').whereIn('user_id', usersIds).as('spu'),
'spu.subscription_product_id', 'sp.id'
)
.leftJoin('products AS prod', 'prod.id', 'sp.product_id')
.leftJoin('properties AS props', 'props.id', 'subs.property_id')
.leftJoin('subscription_product_facility_units AS spfu', 'spfu.subscription_product_id', 'sp.id')
.leftJoin('facility_units AS fu', 'fu.id', 'spfu.facility_unit_id')
const productsDataBySubsciptionIdUserId = subscriptionsDataAggregate.reduce((h, datum) => {
assert.number(datum.subscription_id, 'datum.subscription_id')
assert('product_id' in datum, 'datum.product_id')
assert('user_id' in datum, 'datum.user_id')
const subscriptionId = datum.subscription_id
const productId = datum.product_id
const userId = datum.user_id
if (productId === null) {
return h
}
if (userId === null) {
return h
}
assert.number(productId, 'datum.product_id')
assert('product_name' in datum, 'datum.product_name')
assert('facility_unit_id' in datum, 'datum.facility_unit_id')
assert('facility_unit_name' in datum, 'datum.facility_unit_name')
const product = {
productId: datum.product_id,
productName: datum.product_name,
unitId: datum.facility_unit_id,
unitName: datum.facility_unit_name
}
if (!(subscriptionId in h)) {
h[subscriptionId] = []
}
if (!(userId in h[subscriptionId])) {
h[subscriptionId][userId] = []
}
h[subscriptionId][userId].push(product)
return h
}, {})
const listing = users.map(user => {
const datumForThisUser = subscriptionsDataAggregate
.filter(datum => user.id === datum.user_id)
const subscriptions = subscriptionsIds.map(id => {
assert.number(id, 'id')
return subscriptionsDataAggregate.find(datum => {
assert.number(datum.subscription_id, 'datum.subscription_id')
return id === datum.subscription_id
})
}).map(datum => {
assert('subscription_id' in datum, 'datum.subscription_id')
assert('user_id' in datum, 'datum.user_id')
assert('subscription_status' in datum, 'datum.subscription_status')
assert('subscription_agreement_no' in datum, 'datum.subscription_agreement_no')
assert('subscription_type' in datum, 'datum.subscription_type')
assert('subscription_start_date' in datum, 'datum.subscription_start_date')
assert('subscription_end_date' in datum, 'datum.subscription_end_date')
assert('property_name' in datum, 'datum.property_name')
const isPic = Boolean(picDataAggregate.find(pic => {
assert.number(pic.user_id, 'pic.user_id')
assert.number(pic.subscription_id, 'pic.subscription_id')
return pic.user_id === user.id && pic.subscription_id === datum.subscription_id
}))
const products = datum.subscription_id in productsDataBySubsciptionIdUserId
? productsDataBySubsciptionIdUserId[datum.subscription_id][datum.user_id] || []
: []
return {
id: datum.subscription_id,
status: datum.subscription_status,
agreementNo: datum.subscription_agreement_no,
type: datum.subscription_type,
startDate: datum.subscription_start_date,
endDate: datum.subscription_end_date,
venue: datum.property_name,
isPic,
products
}
})
return {
id: user.id,
name: user.full_name,
email: user.email,
subscriptions
}
})
return Common.mysqlTextRowsToObjects(listing)
}
错误出现在这部分代码中,
static async countMembers(params, options) {
assert.object(params, 'params')
assert.object(options, 'options')
assert.func(options.db, 'options.db')
const query = this.listMembers(params, options)
const countQuery = await query.clone().count();
const totalItems = countQuery[0]['count(*)']
return totalItems
}
请任何人可以帮助我解决此问题?
我检查了几个堆栈溢出问题的相同问题,但没有成功!
以下是listMembers和countMembers的调试输出,
START RequestId:c3f34306-64c1-42ca-8ee4-148323d7b26f版本:$ LATEST
{“服务”:“订阅服务”,“级别”:“信息”,“消息”:“传入
要求\“ c3f34306-64c1-42ca-8ee4-148323d7b26f \”采取行动
\“ search-members \”。“,”时间戳“:” 2020-02-18T06:01:26.691Z“}
{“服务”:“订阅服务”,“级别”:“信息”,“消息”:“ [c3f34306-64c1-42ca-8ee4-148323d7b26f]
SearchMember处理程序已
被调用。“,”时间戳“:” 2020-02-18T06:01:26.750Z“}
{“服务”:“订阅服务”,“级别”:“信息”,“消息”:“ [c3f34306-64c1-42ca-8ee4-148323d7b26f]
授权成功。“,”时间戳“:” 2020-02-18T06:01:27.129Z“}
{“服务”:“订阅服务”,“级别”:“信息”,“消息”:“ [c3f34306-64c1-42ca-8ee4-148323d7b26f]
参数已经过去
验证。“,”时间戳“:” 2020-02-18T06:01:27.171Z“}
{“服务”:“订阅服务”,“级别”:“信息”,“消息”:“ [c3f34306-64c1-42ca-8ee4-148323d7b26f]
成员已成功获取
清单。“,”时间戳“:” 2020-02-18T06:01:28.410Z“}
{“服务”:“订阅服务”,“级别”:“错误”,“消息”:“ [c3f34306-64c1-42ca-8ee4-148323d7b26f]
无法计算数量
成员。“,”时间戳“:” 2020-02-18T06:01:28.412Z“}
{“服务”:“订阅服务”,“级别”:“错误”,“消息”:“未处理”
例外:TypeError:query.clone不是函数\ n,位于
Function.countMembers(/var/task/utils/search_members.js:184:36)\n
在countMembers(/var/task/handlers/search_members.js:91:30)\n在
对象处理程序
(/var/task/handlers/search_members.js:21:28)","timestamp":"2020-02-18T06:01:28.412Z“}
2020-02-18T06:01:28.529Z c3f34306-64c1-42ca-8ee4-148323d7b26f ERROR调用错误{“ errorType”:“ Error”,“ errorMessage”:“”,“ stack”:[“ Error:
“,”在Function.serverError
(/var/task/utils/errors.js:24:12)“,”在Function.handleAppError
(/var/task/utils/errors.js:44:24)“,”在Runtime.handler上
(/var/task/index.js:14:12)“]} END RequestId:
c3f34306-64c1-42ca-8ee4-148323d7b26f REPORT RequestId:
c3f34306-64c1-42ca-8ee4-148323d7b26f持续时间:3628.21 ms已结算
持续时间:3700 ms内存大小:128 MB使用的最大内存:121 MB初始化
持续时间:819.80 ms
listMembers的输出(无countMembers)
{"members":[{"id":2959,"name":"Malith Fernando","email":"[email protected]","subscriptions":[{"id":1375,"status":"void","agreement_no":"GT1234567890","type":1,"start_date":"2019-10-31T16:00:00.000Z","end_date":"2019-12-30T16:00:00.000Z","venue":"Common Ground G Tower","is_pic":true,"products":[]},{"id":1380,"status":"void","agreement_no":"PMTest3","type":1,"start_date":"2019-11-30T16:00:00.000Z","end_date":"2019-12-30T16:00:00.000Z","venue":"Common Ground G Tower","is_pic":true,"products":[]},{"id":1420,"status":"confirmed","agreement_no":"AETest","type":1,"start_date":"2020-02-29T16:00:00.000Z","end_date":"2020-03-30T16:00:00.000Z","venue":"Common Ground Eco City","is_pic":true,"products":[{"product_id":40,"product_name":"Hot Desk","unit_id":null,"unit_name":null}]},{"id":1421,"status":"confirmed","agreement_no":"PrimeMemberTest","type":1,"start_date":"2020-02-29T16:00:00.000Z","end_date":"2020-03-30T16:00:00.000Z","venue":"Common Ground Menara UEM","is_pic":true,"products":[]},{"id":1422,"status":"confirmed","agreement_no":"AE123","type":1,"start_date":"2020-02-29T16:00:00.000Z","end_date":"2020-03-30T16:00:00.000Z","venue":"Common Ground KL Sentral","is_pic":true,"products":[{"product_id":41,"product_name":"Fixed Desk","unit_id":2105,"unit_name":"FD - A2"}]},{"id":1367,"status":"confirmed","agreement_no":"DEMO0000001","type":1,"start_date":"2019-12-31T16:00:00.000Z","end_date":"2020-05-30T16:00:00.000Z","venue":"Common Ground Bukit Bintang","is_pic":false,"products":[{"product_id":42,"product_name":"Private Office","unit_id":4402,"unit_name":"WW51 - 4"}]},{"id":1419,"status":"confirmed","agreement_no":"UEMPM","type":1,"start_date":"2020-02-29T16:00:00.000Z","end_date":"2020-03-30T16:00:00.000Z","venue":"Common Ground Menara UEM","is_pic":false,"products":[{"product_id":143,"product_name":"Prime Membership","unit_id":null,"unit_name":null}]}]}]}
最佳答案
下面的代码更改解决了我的问题,
const countQuery = await db('users').count('*').where(
function () {
this.where('users.full_name', 'like', '%' + query + '%')
.orWhere('users.email', 'like', '%' + query + '%')
}
)
const totalItems = countQuery[0]['count(*)']
关于javascript - JavaScript返回克隆不是countMembers的函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60274319/