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

Get Properties of a relationship #29

Closed
ChristopherCODE14 opened this issue Jun 16, 2021 · 5 comments
Closed

Get Properties of a relationship #29

ChristopherCODE14 opened this issue Jun 16, 2021 · 5 comments
Labels
how-to A question about how to use Neogma

Comments

@ChristopherCODE14
Copy link

ChristopherCODE14 commented Jun 16, 2021

Good morning,

I have set properties to relationships between two nodes. How can I fetch these properties?

I have tried the following so far and cannot find anything in the documentation:

new QueryBuilder() .match({ related: [ { model: Products, where: { id: id, }, identifier: 'products', }, { ...Products.getRelationshipByAlias(alias), identifier: alias, }, { model: model, identifier: 'model', }, ], }) .return([ 'products', 'model', ]) .run(neogma.queryRunner);

Another option that I have tried is to use findRelationships:

const relationships:any = await Products.findWithRelationships({ alias: alias, where: { source: { id: id1 }, target: { id: id2 }, }, limit: 1 });

However, I here I get the following error message: Product_1.Products.findWithRelationships is not a function

Do you know how to solve the issue and how to make the second code example run, as I also need it for other purposes?

@themetalfleece
Copy link
Owner

Hey Christopher,

For the first query you can change your return to include alias, like: .return([ 'products', 'model', alias]
Then you need to access the records of the returned value (i.e. const res = await new QueryBuiler(...) to get the properties of the relationship, like res.records[0].get(alias).properties

I don't know why the second example is not running (assumming you called findRelationships, not findWithRelationships). For debugging, can you check out what happens if you call relateTo instead?

@ChristopherCODE14
Copy link
Author

Hey Jason,

thanks for your response.

I have included alias into the QueryBuilder instance. However, it is still not sending the properties of the relationship and I am also not sure how to include the res.records[0].get(alias).properties into my code. Can you tell me where to include it?

I run the following but I dont get the properties however:
const data: any = QueryRunner.getResultProperties<any>( getSourceRelationship, 'model', );

Do I need to include it into the displayed code snippet or somewhere else?

For the second issue:

When I change findWithRelationships to findRelationships I get the same error.

I already use relateTo to create the relationship:

await product.relateTo({ alias: "HasAlternativeProduct", where: { id: req.params.changeId }, properties: { GreenPremium: greemPremium, }, })

Looking forward to your response!

@themetalfleece
Copy link
Owner

@ChristopherCODE14 for the first part, try the following:

const res = await new QueryBuilder()
.match({ related: [ { model: Products, where: { id: id, }, identifier: 'products', }, { ...Products.getRelationshipByAlias(alias), identifier: alias, }, { model: model, identifier: 'model', }, ], })
.return([ 'products', 'model', alias])
.run(neogma.queryRunner);

console.log(res.records[0]?.get(alias).properties);

For the second part, I was wondering what happens if in the exact same place you're calling await Products.findRelationships, you call await Products.relateTo instead. Does it still throw an error?

@ChristopherCODE14
Copy link
Author

Good morning,

yes it is working finally. Thanks for your help!

@themetalfleece themetalfleece added the how-to A question about how to use Neogma label Jun 21, 2021
@themetalfleece
Copy link
Owner

Glad to hear that! Please feel free to create more issues for bugs and suggestions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
how-to A question about how to use Neogma
Projects
None yet
Development

No branches or pull requests

2 participants