Skip to content

Checking if a value exists in a Record #82

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

Closed
legraphista opened this issue Jun 15, 2016 · 2 comments
Closed

Checking if a value exists in a Record #82

legraphista opened this issue Jun 15, 2016 · 2 comments

Comments

@legraphista
Copy link
Contributor

legraphista commented Jun 15, 2016

There is no "proper" way to check if a value exists or not in a Record without trying to access and catching for errors.

Take this oversimplified example:

const queries = [
   'MATCH (a:Apple) RETURN COUNT(a) AS apples' ,
   'MATCH (a:Apple), (o:Orange) RETURN COUNT(a) AS apples, COUNT(o) AS oranges'
];

const query = queries[Math.random() > .5 ? 1 : 0];

session.run(query).then((data) => {
    console.log(data[0].get('apples'));

    // will throw if first query is chosen
    console.log(data[0].get('oranges')); 
});

There should be a way of checking for fields before trying to use them:

session.run(query).then((data) => {
    console.log(data[0].get('apples'));

    // will throw if first query is chosen
    if(data[0].has('oranges')) {
        console.log(data[0].get('oranges')); 
    }
});
@legraphista
Copy link
Contributor Author

legraphista commented Jun 15, 2016

I've made a pull request with the necessary code and tests to implement Record.has(key) here #83

@pontusmelke
Copy link
Contributor

I agree this is missing in the driver, and I am happy to use your PR.

pontusmelke added a commit that referenced this issue Jun 21, 2016
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