Skip to content

TypeError in cache database sync due to unparsed JSON response bodies #21

Description

@SithuKyaw-Thitsaworks

Problem

The application is experiencing TypeError: Cannot read properties of undefined errors when trying to access nested properties of quoteResponse.body and fxQuoteRequest.conversionTerms.

Root Cause

The quoteResponse.body is stored as a string in the cache database but is not being parsed as a JSON object before property access attempts.
This causes failures when trying to access nested properties like quoteResponse?.body?.transferAmount.amount.

Error Details

TypeError: Cannot read properties of undefined (reading 'amount')
at cacheKey (/opt/mojaloop-payment-manager-experience-api/src/lib/cacheDatabase/index.js:149:69)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

Image

TypeError: Cannot read properties of undefined (reading 'conversionId')
at cacheKey (/opt/mojaloop-payment-manager-experience-api/src/lib/cacheDatabase/index.js:188:67)

Image

Affected Code Locations

https://github.com/pm4ml/mojaloop-payment-manager-experience-api/blob/7d3626b2cd2c63b3771003651fa78fd59b5be890/src/lib/cacheDatabase/index.js#L149

https://github.com/pm4ml/mojaloop-payment-manager-experience-api/blob/7d3626b2cd2c63b3771003651fa78fd59b5be890/src/lib/cacheDatabase/index.js#L188

Evidence

Log analysis shows that quoteResponse.body contains a JSON string rather than a parsed object:

Image

Proposed Solution

  • Enhance the parseData function to consistently parse all response body fields that may be stored as JSON strings

  • Add JSON parsing for quoteResponse.body, fxQuoteRequest.body, fxQuoteResponse.body, and other similar fields

  • Implement error handling around JSON parsing operations to prevent crashes

  • Use optional chaining (?.) and null coalescing (??) operators for all nested property access

  • Add validation checks to ensure objects exist before attempting to access their properties

Steps to Reproduce

  1. Execute transfer operations that involve quote responses
  2. Observe the TypeError when accessing nested properties of cached quote response data

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions