Friday, 12 May 2017

Get string query with mongoose and angular uib-typeahead

I am trying to use uib-typeahead from uib-bootstrap and angular.

When I type something as "acrel" into input typeahead, I'd expect receive the same string into request param from my mongo server using mongoose and router.

When I look at server side, the string typed appears as a object: { '0': 'a', '1': 'c', '2': 'r', '3': 'e', '4': 'l' }.

I need a string on server side query as the original "acrel".

How can I solve it?

//server log to "request"

  baseUrl: '',
  originalUrl: '/cidades?0=a&1=c&2=r&3=e&4=l',
  _parsedUrl:
   Url {
     ....
     search: '?0=a&1=c&2=r&3=e&4=l',
     query: '0=a&1=c&2=r&3=e&4=l',
     pathname: '/cidades',
     path: '/cidades?0=a&1=c&2=r&3=e&4=l',
     href: '/cidades?0=a&1=c&2=r&3=e&4=l',
     _raw: '/cidades?0=a&1=c&2=r&3=e&4=l' },
  params: {},
  query: { '0': 'a', '1': 'c', '2': 'r', '3': 'e', '4': 'l' },
  res:
   ServerResponse {

//server with mongoose

'use strict';
const express = require('express');
const router = express.Router();
//const querystring = require('querystring');
const Cidade = require('../models/cidade'); 
const callback=function(err,data,res){
     if (err) return res.status(500).json(err);
     return res.status(200).send(data);
}

router.getCidade=function(req,res,next){
    console.log(req);
    const query=new RegExp(req.query,'i');
    Cidade.find({ cidades: query }, (err,data) => {
       callback(err,data,res)
    })
}

module.exports=router;

//markup

<script type="text/ng-template" id="mycustomTemplate.html">
                                          <a>-</a>
</script>
<div class="col-sm-5 col-lg-4">
        <input name="cidade.nome" id="nome" type="text" ng-model="clinica.cidade" placeholder="Digite o nome da cidade" autocomplete="off"
         uib-typeahead="cidade as cidade for cidade in getCidades($viewValue)" typeahead-loading="loadingLocations" typeahead-no-results="noResults"
class="form-control" typeahead-template-url="mycustomTemplate.html">
</div>

~çasxa



via Luiz Alves

No comments:

Post a Comment