How to Use Ember.js with MongoDB for Querying Data Using "AND"

I've been making great progress on my Emberjs application, but I've hit a roadblock that's leaving me stumped. Despite scouring the web for answers, I can't seem to find a solution.

The issue at hand involves a dropdown feature in my app that queries records based on user selection. For example, there is a "Dental" Department with a service called "Braces." When a user changes the dropdown option, a query filters the results accordingly. Within these results, there is a text field that should allow users to search within the displayed services specific to the selected department.

Here's where things get tricky. The search function disrupts the condition for services belonging to the selected department. Is there a way to implement an 'AND' operator in my Ember controller to query records with multiple conditions?

Take a look at my Template:

<div class="form-group">
  {{#power-select
options=departments
selected=selectedDepartment
searchField="name"
placeholder="Select Department..."
onchange=(action (mut selectedDepartment))
dropdownClass="in-modal-dropdown"
renderInPlace=true
as |department|
  }}
      {{department.name}}
  {{/power-select}}
 </div>
 {{#if selectedDepartment}}
 <hr />
  <div class="form-group has-feedback">
   {{input value=searchText class="form-control input-sm" placeholder="Search Services" insert-newline="doSearch"}}
     {{#if searchText}}
       <i class="glyphicon glyphicon-remove form-control-feedback"></i>
     {{/if}}
  </div>
  <br />
  {{#each departmentServices as |service|}}
    <button {{action 'selectService' service}} class="ux-product-override-for-request w-clearfix w-inline-block">
      <div class="ux-product-icon"></div>
      <div class="ux-product-title">{{service.name}}</div>
      <div class="ux-product-price">{{service.price}} RS</div>
     </button>
  {{/each}}
 {{/if}}

Now, let's take a look at my Controller:

store: Ember.inject.service(),
departments: Ember.computed(function(){
return this.get('store').findAll('department')
}),
departmentServices: Ember.computed('selectedDepartment', 'search', function(){
if(this.get('search') == '' || this.get('search') == null){
console.log(this.get('search'));
return this.get('store').query('service', {
where: {
department: this.get('selectedDepartment.id')
}
})
} else {
return this.get('store').query('service', {
where: {
{ department: { this.get('selectedDepartment.id')} }
{ name: { contains: this.get('search')} }
}
})
}
}),
selectedDepartment: null,

Answer №1

{{input value=searchText - in this snippet, you are utilizing searchText, while in the departmentServices computed property, you are using search, however, that is not the root cause of the issue.

The main problem lies in the fact that this.get('store').query will return a Promise instead of an actual value, making your implementation ineffective. (To resolve this, you can consider exploring the 3rd option "Special promise-backed objects" as outlined at )

I suggest creating a setDepartmentServices function to handle querying and updating the departmentServices property. Additionally, utilize power-select for onchange events instead of relying on the mut. You can replace it with

(onchange = (action 'setSelectedDepartment')
, and when inputting text in the searchText field, calling doSearch to trigger setDepartmentServices. Here's a basic outline:

setDepartmentServices() {
    // This function executes the query and updates the departmentServices property.
}
actions: {
    setSelectedDepartment(selectedDepartment) {
        this.set('selectedDepartment', selectedDepartment);
        // Check if it's suitable to update the departmentServices
        this.send('setDepartmentServices');
    }
    doSearch() {
        // Check if it's appropriate to update the departmentServices
        this.send('setDepartmentServices');
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Highlighting Search Results in Kendo TreeView

I am currently working on a KendoTreeview project with spriteclass. My goal is to highlight both the root and child nodes with a specific search term. I have successfully implemented the search functionality, however, there is an issue that arises after th ...

Navigating to a particular value in Vue: A step-by-step guide

In my Vue application, I have a basic table structure: <tbody> <tr v-for="(item, index) in items"> <td> ... </td> </tr> </tbody> The items are dynamically added using the unsh ...

Storing dates as collection names is not supported in Firestore

I'm currently facing an issue trying to store stock prices in Firestore. I want the structure to resemble something similar to SQL, like this: const d1 = new Date(); const result = d1.getTime(); console.log('Epochtime',result); database.coll ...

Tips for integrating and showcasing API data in your React JS application by utilizing React Table and Axios

I am just starting out with React and I am faced with the task of fetching data from an API and displaying it in a table. I have decided to use React Table for this purpose. However, I am encountering some issues with getting the data from the API to sh ...

What steps should be taken to generate a successful pop-up window post registration in PHP?

beginning section continuation What is the best way to design an effective popup window? ...

What is the best method for interpreting XML using JavaScript?

I am facing a challenge with fetching and parsing an XML file using JavaScript. The XML-file is beyond my control. Recently, there has been a change in the encoding of some XML files which prevents the code from being parsed successfully. Previously it wa ...

What is the best way to retrieve all SVG objects within a specific area in an Angular application?

I am currently developing an SVG drawing application and have implemented a tool that enables users to select all shapes within a rectangular area. However, I am facing the challenge of detecting the SVG shapes located underneath the selected rectangle. ...

Learn how to trigger an HTTP exception after a failed command in a saga with NestJS CQRS

Currently utilizing the NestJS CQRS pattern to handle interactions between User and UserProfile entities within my system. The setup consists of an API Gateway NestJS server along with dedicated NestJS servers for each microservice (User, UserProfile, etc. ...

Utilizing a JavaScript variable to fetch a rails URL: A comprehensive guide

One interesting feature I have is an image link that has a unique appearance: <a href="#user-image-modal" data-toggle="modal" data-id="<%= image.id %>"><img class="user-photo" src="<%= image.picture.medium.url %>" alt="" /></a&g ...

All You Need to Know About Jquery's Selector for Nested Elements

Could someone clarify if the Jquery Statement $(selecterA:not(selectorB), elementA) means "return those elements that match selectorA but not selectorB within elementA"? I am confused by a simple case in Fiddle#1, where both output statements using .length ...

Oops! We're having trouble locating the react-redux context value. Make sure to wrap the component in a <Provider> to fix this issue. It's all about using Redux with

My debut Next.js website integrated with Redux is running into an issue with the following error: Error: could not find react-redux context value; please ensure the component is wrapped in a The setup involves using _document.js to establish a 'tem ...

After receiving the go-ahead from JavaScript, I am planning on integrating PHP into my project. I have come across some recommendations

Looking for assistance with AJAX functionality on a website. Specifically, users should be prompted to confirm a purchase before completing it. If they confirm, the purchase goes through; if they decline, it does not. Originally considered using PHP within ...

What are the benefits and drawbacks of combining Jquery UI with AngularJS in web development?

Currently, I am developing a Web application and considering integrating JqueryUI and AngularJS into my ASP.NET MVC project. Is this the best decision for my project? Are there any recommended Databinding libraries that can be used with JQuery UI? Please ...

The JavaScript replace function using regex eliminates additional content

There is a content string that includes full YouTube URLs and video IDs. I need to replace the URLs with just the video IDs. The example of the "content" variable: var content = '{GENERICO:type="youtube",id="DluFA_AUjV8"}{GENERICO:type="youtube",id ...

Updating multiple documents concurrently in Java using MongoDB is a breeze

I have a specific schema that looks like this: Id: date_created: vars: { NAME: VALUE: EDITABLE: ..... } According to the definition, Id is considered to be unique and can vary from values such as A, B, C, AA, AB, etc. I posse ...

What steps should be taken to ensure the proper functioning of og: meta tags in NextJS?

Adding OpenGraph meta tags to a page in my NextJS app has presented some challenges. I inserted the meta tags within the <Head> component that is accessible through next/head. After testing the OpenGraph data with tools like the Facebook Sharing Deb ...

Having trouble with CORS in your Angular application?

I am attempting to retrieve data from a site with the URL: Using the $http service After extensive research, I have come up with this CoffeeScript code snippet: angular.module('blackmoonApp') .controller 'PricingCtrl', ($scope, $ht ...

Push function is not available in Redux

Currently, I am facing an issue while trying to use the state of a component in other components. Since this state needs to be accessed by multiple components, I have decided to switch to Redux. However, I encountered an error 'newUsers.push is not a ...

Creating a task list without using JavaScript in the web browser and without relying on a database

Looking for some guidance on building a todo app for a job interview where JavaScript is disabled in the browser and no database can be used. Any JavaScript needs to be handled on the server side. I have some basic knowledge of node/express and serving H ...

Troubleshooting: Clicking the Ajax button yields no response

I’ve looked through all the similar questions and tried multiple solutions, but nothing seems to be working. My goal is to retrieve job postings from my database related to careers. When a user wants to apply for a job, they should click a button which w ...