Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_findHandler, return data from post middleware, instead of the original result #188

Open
li5tun opened this issue Jul 26, 2019 · 0 comments

Comments

@li5tun
Copy link
Contributor

li5tun commented Jul 26, 2019

Is your feature request related to a problem? Please describe.
When I changed a field value(or add some other logic) in post, I want the changed object as the final result.

Describe the solution you'd like
return data instead of result

Describe alternatives you've considered
N

Additional context
`async function _findHandler(model, _id, request, Log) {
try {
let query = Object.assign({}, request.query)
try {
if (
model.routeOptions &&
model.routeOptions.find &&
model.routeOptions.find.pre
) {
query = await model.routeOptions.find.pre(_id, query, request, Log)
}
} catch (err) {
handleError(err, 'There was a preprocessing error.', Boom.badRequest, Log)
}

let flatten = false
if (query.$flatten) {
  flatten = true
}
delete query.$flatten
let mongooseQuery = model.findOne({ _id: _id })
mongooseQuery = QueryHelper.createMongooseQuery(
  model,
  query,
  mongooseQuery,
  Log
).lean()
let result = await mongooseQuery.exec()
if (result) {
  let data = result
  try {
    if (
      model.routeOptions &&
      model.routeOptions.find &&
      model.routeOptions.find.post
    ) {
      data = await model.routeOptions.find.post(request, result, Log)
    }
  } catch (err) {
    handleError(
      err,
      'There was a postprocessing error.',
      Boom.badRequest,
      Log
    )
  }
  if (model.routeOptions) {
    let associations = model.routeOptions.associations
    for (let associationKey in associations) {
      let association = associations[associationKey]
      if (association.type === 'ONE_MANY' && data[associationKey]) {
        // EXPL: we have to manually populate the return value for virtual (e.g. ONE_MANY) associations
        result[associationKey] = data[associationKey]
      }
      if (association.type === 'MANY_MANY' && flatten === true) {
        // EXPL: remove additional fields and return a flattened array
        if (result[associationKey]) {
          result[associationKey] = result[associationKey].map(object => {
            object = object[association.model]
            return object
          })
        }
      }
    }
  }

  if (config.enableSoftDelete && config.filterDeletedEmbeds) {
    // EXPL: remove soft deleted documents from populated properties
    filterDeletedEmbeds(result, {}, '', 0, Log)
  }

  Log.log('Result: %s', JSON.stringify(result))

  return result
} else {
  throw Boom.notFound('No resource was found with that id.')
}

} catch (err) {
handleError(err, null, null, Log)
}
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant