question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Building in a loop

See original GitHub issue

Hi,

I am looking to construct the body in a loop and build all of it at once. Is it feasible.

Here is my code.

    filters.forEach((filter) => {
      switch (filter.type) {
        case 'freetext':
          if (filter.filteredValue) {
            if (filter.condition === 'includes') {
              bodybuilder().addQuery('match', filter.field, filter.filteredValue);
            } else {
              bodybuilder().notQuery('match', filter.field, filter.filteredValue);
            }
          }
          break;
        case 'singleselect':
          if (filter.condition === 'includes') {
            bodybuilder().addQuery('match', filter.field, filter.filteredValue);
          } else {
            bodybuilder().notQuery('match', filter.field, filter.filteredValue);
          }
          break;
        case 'multiselect':
          filter.filteredValue.forEach((value) => {
            if (filter.condition === 'includes') {
              bodybuilder().addFilter('term', filter.field, value);
            } else {
              bodybuilder().notFilter('term', filter.field, value);
            }
          });
          break;
        case 'search':
          filter.filteredValue.forEach((field) => {
            if (filter.splitFilteredValue) {
              if (filter.condition === 'includes') {
                bodybuilder().addFilter('term', filter.splitColumns[0], field.value.split()[0]);
                bodybuilder().addFilter('term', filter.splitColumns[1], field.value.split()[1]);
              } else {
                bodybuilder().notFilter('term', filter.splitColumns[0], field.value.split()[0]);
                bodybuilder().notFilter('term', filter.splitColumns[1], field.value.split()[1]);
              }
            } else {
              if (filter.condition === 'includes') {
                bodybuilder().addFilter('term', filter.field, field.value);
              } else {
                bodybuilder().notFilter('term', filter.field, field.value);
              }
            }
          });
          break;
        case 'numberrange':
          break;
        case 'daterange':
          break;
        default:
      }
    });
    console.log(bodybuilder().build());

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
danpazcommented, Apr 30, 2020

@pachu4u that’s correct, I missed that detail in your code snippet. You need to operate on the same bodybuilder instance with every addQuery.

0reactions
pachu4ucommented, Apr 30, 2020

For the benefit of others

You will have to save the bodybuilder object first and then use the same for adding queries & filters. Like so

Correct approach:

bb=bodybuilder() bb.addQuery(‘match’, ‘message’, ‘this is a test’) bb.addQuery(‘match’, ‘message’, ‘this is a test’) bb.build()

Incorrect approach: bodybuilder().addQuery(‘match’, ‘message’, ‘this is a test’) bodybuilder().addQuery(‘match’, ‘message’, ‘this is a test’) bodybuilder().build()

Although I thought bodybuilder is a function it seems to be creating an object everytime.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build Loop App - LoopDocs - GitHub Pages
Watch in awe as you build your very own Loop app. FAQs. The FAQs for building Loop are in-line with the steps that...
Read more >
Build A Loop - Swift Playgrounds - YouTube
... videos: http://www.buildingrainbows.comSolution for the " Build a Loop " activity in the Learn to Code chapter "World Building " in Apple'...
Read more >
Starting Loop: Build | Loop and Learn
If you've decided to build Loop, the easiest method is to use the Build-Select script. This script works for master and FreeAPS versions...
Read more >
Build a for loop from scratch | R - DataCamp
Here is an example of Build a for loop from scratch: This exercise will not introduce any new concepts on for loops.
Read more >
The trust-building loop | Download Scientific Diagram
way of thinking about trust building is through the loop depicted in Fig. 2. This argues that two factors are important in getting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found