jsonpath
jsonpath copied to clipboard
this.nodes is not a function
Hi, Premise:
- 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.
- I built the xyz-component library with npm_pack and a xyz-component.0.0.1.tgz is created.
- I deploy the tgz file with npm install in my consuming project for e.g. consumingmain-portal everything goes fine.
- Did a npm install of jsonpath in the consuming project as well
- 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 node_modules\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
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..
in my case I have to import jsonpath in the following way
const jsonpath = require("jsonpath")
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'
I had the same issue because I was calling :
jp.query.call(null, data, path)When I changedthisparameter fromnulltojpit 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.
I think this needs to be fixed so that tree shaking can be done to only use functions that are required..