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

Full doc listener? #51

Closed
mwkrieger opened this issue Jun 28, 2017 · 7 comments
Closed

Full doc listener? #51

mwkrieger opened this issue Jun 28, 2017 · 7 comments

Comments

@mwkrieger
Copy link

I understand how to add an expressa.addListener to each document, but I would like to know if there is a way to add a listener to expressa that happens right before the final response is sent.

I'd like to add a copyright message to the end of my entire result before it's sent to the client.

Can't quite figure it out. :/

@thomas4019
Copy link
Owner

I think you can just add in the copyright to the json document in a "get" listener. I'll look into adding another listener for right before sending, but I don't think that's necessary for this.
I'm not sure exactly how you're going to add the copyright. Are you tg

@thomas4019
Copy link
Owner

Sorry I posted too soon by accident. Are you going to add a field to the output or are you going to add a header. Otherwise, just adding text before or after would break Jon parsing.

@mwkrieger
Copy link
Author

Cool, thanks.

Let's say I have several docs. Right now, they come in as an array. I'd like to be able to add everything to a root item like 'data'.

Then, at the end, I'd like to add a copyright statement off the root. Something like 'copyright' :'message'.

Having this global feature would let me manipulate the final json before its sent out, not just the document inside the array.

Make sense?

thomas4019 added a commit that referenced this issue Jun 29, 2017
@thomas4019
Copy link
Owner

Yep, that makes perfect sense. Thanks for the detailed explanation! I've just committed a "getpresend" listener that does what you're looking for, but I haven't pushed out a new npm version because I'm not sure about the name yet.

@mwkrieger
Copy link
Author

Nice! Let me take a look at it and I'll come back to you.

What's in a name, right? :)

@mwkrieger
Copy link
Author

mwkrieger commented Jun 30, 2017

Hey again.

I can see the event firing correctly, but it doesn't seem as though I can manipulate the doc and pass it back to the response. Here's my bit of code:

expressa.addListener('getpresend',1, function(req, collection, doc) {
    return new Promise(function(resolve, reject) {
         doc = JSON.parse(JSON.stringify(doc).split('"Title":').join('"new_title":'));
            console.log(doc) // **<-- this works and prints out the expected result to the console**
            resolve() // **<-- I'd expect this to return the modified doc, but instead, it returns the original.**
        })
})

I can't seem to manipulate and return the doc as I would expect to. Any suggestions on what I'm doing wrong?

@thomas4019
Copy link
Owner

I think the problem here is that you're replacing the "doc" variable. Since JavaScript passes by value, the caller of your listener function doesn't see the change. There's two potential solutions.

  1. Use Object.assign
expressa.addListener('getpresend',1, function(req, collection, doc) {
  modified = JSON.parse(JSON.stringify(doc).split('"status":').join('"status222":'));
  Object.assign(doc, modified)
})
  1. Use something like this function instead of doing string replacement.

Also, note that since you're not doing any async operations (e.g. file IO, network requests, database lookups) the promise is unnecessary.

coderofsalvation pushed a commit to coderofsalvation/expressa that referenced this issue Aug 16, 2017
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

2 participants