dchester / jsonpath

Query and manipulate JavaScript objects with JSONPath expressions. Robust JSONPath engine for Node.js.
MIT License
1.34k stars 215 forks source link

this.nodes is not a function #126

Open pankajashankar opened 5 years ago

pankajashankar commented 5 years ago

Hi, Premise:

  1. I have a angular 7 custom component library xyz-component. Within the library in one of the child component I use jsonpath which I installed with npm install jsonpath and the version of 1.0.1 got installed which I verified in the package.json.
  2. I built the xyz-component library with npm_pack and a xyz-component.0.0.1.tgz is created.
  3. I deploy the tgz file with npm install in my consuming project for e.g. consumingmain-portal everything goes fine.
  4. Did a npm install of jsonpath in the consuming project as well
  5. When I try to run the consuming application - with ng serve I get the error _node_modules\jsonpath\lib\index.js change all references to'this' to JSONPath.prototype - resolve ‘ this.nodes is not a function’ issue. Note: change is in node_modules of consuming project__

Not sure why it gives that error but when I modify the index.js under nodemodules\jsonpath to change this to JSONPath.prototype, and re-run I do not get the error.

Please let me know what is the issue here. Also mainly what is the solution.

--Thanks

elmpp commented 5 years ago

think the answer to this is to follow the docs exactly:

// https://github.com/dchester/jsonpath#jpqueryobj-pathexpression-count

import jp from 'jsonpath'
jp.query(BLAH)

If you destructure the import (as i was):

import {query} from 'jsonpath'
query(BLAH)

It gives the error you describe. I'd say it was down to webpack treeshaking or something but declaring as an external didn't help either..

guoliang commented 3 years ago

in my case I have to import jsonpath in the following way

const jsonpath = require("jsonpath")

danbars commented 3 years ago

I had the same issue because I was calling : jp.query.call(null, data, path) When I changed this parameter from null to jp it solved the problem. jp.query.call(jp, data, path)

btw, my import is import jp from 'jsonpath'

guoliang commented 3 years ago

I had the same issue because I was calling : jp.query.call(null, data, path) When I changed this parameter from null to jp it solved the problem. jp.query.call(jp, data, path)

btw, my import is import jp from 'jsonpath'

belated update from my initial post. I did that change recently also and got it to work. so in conclusion it seems like importing individual function does not work, instead you have to import the entire content.

LRagji commented 1 year ago

I think this needs to be fixed so that tree shaking can be done to only use functions that are required..