Home » Java » java – JavaScript return clone is not a function for countMembers-Exceptionshub

java – JavaScript return clone is not a function for countMembers-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m getting all members that equal to name (e.g ‘Malith) with their subscription products. That function is working 100%. Also with that output i’m try to return total records so front end can set paging, limit, offset easily. But i’m getting clone is not a function.

Below is my code

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

Error come in this part of the code,

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
  }

Please anyone can helps me to fix this?

I checked few stack overflow answers for same question but didn’t worked!

Below is debugged output for listMembers and countMembers,

START RequestId: c3f34306-64c1-42ca-8ee4-148323d7b26f Version: $LATEST

{“service”:”subscription-service”,”level”:”info”,”message”:”Incoming
request \”c3f34306-64c1-42ca-8ee4-148323d7b26f\” for action
\”search-members\”.”,”timestamp”:”2020-02-18T06:01:26.691Z”}

{“service”:”subscription-service”,”level”:”info”,”message”:”[c3f34306-64c1-42ca-8ee4-148323d7b26f] The SearchMember handler has been
invoked
.”,”timestamp”:”2020-02-18T06:01:26.750Z”}

{“service”:”subscription-service”,”level”:”info”,”message”:”[c3f34306-64c1-42ca-8ee4-148323d7b26f] Authorization successful.”,”timestamp”:”2020-02-18T06:01:27.129Z”}

{“service”:”subscription-service”,”level”:”info”,”message”:”[c3f34306-64c1-42ca-8ee4-148323d7b26f] The params have passed
validation
.”,”timestamp”:”2020-02-18T06:01:27.171Z”}

{“service”:”subscription-service”,”level”:”info”,”message”:”[c3f34306-64c1-42ca-8ee4-148323d7b26f] Members have been successfully fetched for
listing.
“,”timestamp”:”2020-02-18T06:01:28.410Z”}

{“service”:”subscription-service”,”level”:”error”,”message”:”[c3f34306-64c1-42ca-8ee4-148323d7b26f] Failed to count the number of
members
.”,”timestamp”:”2020-02-18T06:01:28.412Z”}

{“service”:”subscription-service”,”level”:”error”,”message”:”UNHANDLED
EXCEPTION: TypeError: query.clone is not a function
\n at
Function.countMembers (/var/task/utils/search_members.js:184:36)\n
at countMembers (/var/task/handlers/search_members.js:91:30)\n at
Object.handler
(/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 Invoke Error {“errorType”:”Error”,”errorMessage”:””,”stack”:[“Error:
“,” at Function.serverError
(/var/task/utils/errors.js:24:12)”,” at Function.handleAppError
(/var/task/utils/errors.js:44:24)”,” at Runtime.handler
(/var/task/index.js:14:12)”]} END RequestId:
c3f34306-64c1-42ca-8ee4-148323d7b26f REPORT RequestId:
c3f34306-64c1-42ca-8ee4-148323d7b26f Duration: 3628.21 ms Billed
Duration: 3700 ms Memory Size: 128 MB Max Memory Used: 121 MB Init
Duration: 819.80 ms

Output for listMembers (without 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}]}]}]}
How to&Answers: