从angularjs服务调用Mongodb函数

时间:2018-08-06 04:40:34

标签: javascript angularjs mongodb mean-stack

我正在尝试为我的平均堆栈Web应用创建搜索,这是我到目前为止所做的:搜索栏的导航栏格式为btw

product.controller.js(服务器)

exports.search = function(req, res) {
  Produit
    .find({
      $text: {
        $search: req.params.term
      }
    })
    .populate('categories')
    .exec()
    .then(respondWithResult(res))
    .catch(handleError(res));
};

index.js(服务器)

router.get('/:term/search', controller.search);

navbar.component.js

this.search = function() {
      $rootScope.$broadcast('search:term', this.searchTerm);
      console.log('it works !!');
    };

navbar.html

<input id="searchBox" type="text" class="form-control input-large" placeholder="Search" ng-model="searchTerm" ng-change="navCtrl.search()">

product.service.js

function produitsService($resource, $timeout, Upload, $q) {
  var resource = $resource('/api/produits/:id/:controller', null, {
    update: {
      method: 'PUT'
    },
    catalog: {
      method: 'GET',
      isArray: true,
      params: {
        controller: 'catalog'
      }
    },
    search: {
      method: 'GET',
      isArray: true,
      params: {
        controller: 'search'
      }
    },

product.component.js

constructor(Produit, $rootScope) {
    this.produits = Produit;
    $rootScope.$on('search:term', function(event, data) {

        this.produits = Produit.search({id: data});

    });
  }

错误

GET http://localhost:3000/api/produits/search 500 (Internal Server Error)

angular.js:14961 Possibly unhandled rejection: {"data":{"message":"Cast to ObjectId failed for value \"search\" at path \"_id\" for model \"Produit\"","name":"CastError","stringValue":"\"search\"","kind":"ObjectId","value":"search","path":"_id"},"status":500,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/api/produits/search","headers":{"Accept":"application/json, text/plain, */*","X-XSRF-TOKEN":"k8ucUq1x0kickvmqcxfuuGZHxHNXWDzEpWleg="}},"statusText":"Internal Server Error","xhrStatus":"complete","resource":[]}

在控制台上,当我开始在搜索栏上输入内容时,我会收到此提示

GET /api/produits/search 500 20.806 ms - 190
GET /api/produits/search 500 8.705 ms - 190
GET /api/produits/search 500 18.085 ms - 190

0 个答案:

没有答案