Code Monkey home page Code Monkey logo

Comments (7)

thomas4019 avatar thomas4019 commented on May 20, 2024

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

from expressa.

thomas4019 avatar thomas4019 commented on May 20, 2024

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.

from expressa.

mwkrieger avatar mwkrieger commented on May 20, 2024

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?

from expressa.

thomas4019 avatar thomas4019 commented on May 20, 2024

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.

from expressa.

mwkrieger avatar mwkrieger commented on May 20, 2024

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

What's in a name, right? :)

from expressa.

mwkrieger avatar mwkrieger commented on May 20, 2024

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?

from expressa.

thomas4019 avatar thomas4019 commented on May 20, 2024

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.

from expressa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.