Database Search and Display Result Express Node Angular MEAN.js Resource $

I have two models called "Jeans" and "Shirts" which have two variables: "name" and "color"

I want to have a search page where the user can search through the database to find shirts and jeans in a specific color or name.

This link may be a hint, but I just couldn't figure it out Similar problems

I have something here, but it doesn't work. I appreciate it if you can tell me how to fix this. Thank!

View

<section data-ng-controller="AllsController" data-ng-init="find()">
<div class="page-header">
    <h1>Alls</h1>
</div>

<div class="Search">

    <h2>Search Section</h2>

    <select data-ng-model="search1" id="search">
      <option value="Model1">Jeans</option>
      <option value="Model2">Shirts</option>
    </select>

    <select data-ng-model="search2" id="search">
      <option value="Model1">Jeans</option>
      <option value="Model2">Shirts</option>
    </select>

    <select data-ng-model="variable1" id="variable1">
      <option selected="selected">AnyThing</option>
      <option value="color1">Blue</option>
      <option value="color2">Red</option>
    </select>

    <select data-ng-model="variable2" id="variable2">
      <option selected="selected">AnyThing</option>
      <option value="name1">John</option>
      <option value="name2">Bob</option>
    </select>

</div>


<br></br><br></br>

<h2>Result Section</h2>

<div class="list-group">
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>Name</th>
                <th>Color</th>
            </tr>
        </thead>
        <tbody>
                <tr data-ng-repeat="all in alls">   
                <td data-ng-bind="all.name"></td>
                <td data-ng-bind="all.color"></td>
                </tr>
        </tbody>
    </table>

</div>

      

In the controller; first I see which properties the user has selected and assigned it to FindArray Then I see which model user wants to search. (AnyThing means that the user has not selected anything)

Server Controller

exports.list = function(req, res) {
if (variable1 === "AnyThing")
{
    if (variable2 === "AnyThing")
    {
        FindArray = {};
    }else
    {
        FindArray = { name = variable2 };
    }
}else
{
    FindArray = { color = variable1 , name = variable2 };
}

if (req.body.search1 === 'Jeans')
{

    if (req.body.search2 === 'Shirts')
    {
        Jeans.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, jeans) {
            Shirt.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, shirts) {
                var all = shirts.concat(jeans);
                res.jsonp(all);
            });
        });
    }else{
        Jeans.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, jeans) {
                res.jsonp(jeans);
        });
    }
}else{
    Shirt.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, shirts) {
            res.jsonp(shirts);
    });
}
};

      

$ Resource Service:

angular.module('alls').factory('Alls', ['$resource',
function($resource) {
    return $resource('alls/:allId', { allId: '@_id'
    }, {
        update: {
            method: 'PUT'
        }
    });
}]);

      

Client Controller

angular.module('alls').controller('AllsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Alls', 'Jeans', function($scope, $stateParams, $location, Authentication, Alls, Jeans) {
$scope.find = function() {

        $scope.search1 = search1;
        $scope.search2 = search2;
        $scope.variable1 = variable1;
        $scope.variable2 = variable2;

        $scope.alls = Alls.query();

    };
}]);

      

Server side route

module.exports = function(app) {
var alls = require('../../app/controllers/alls.server.controller');

app.route('/alls').get(alls.list);
};

      

+3


source to share


1 answer


A preview of this looks like your route is waiting to be received, but your $ resource is running put. I also think that all of your scopes assignments will not work because you are assigning non-existent variables.



0


source







All Articles