Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
Get all matching items using ids array form database

I can't receive list of items that matches with my array of ids.

This is PART of code in Angular component:

this.orderService.getSpecyficOrders(ids)
    .subscribe(orders => { ... 

Where ids is an array of

[{_id : ID },{_id : ID },{_id : ID },]

ID is "5235sd23424asd234223sf44" kind of string form MongoDB documents.

In angular service file I have imported: Http, Headers, and import 'rxjs/add/operator/map';

Here is code in service in Angular:

getSpecyficOrders(ids){
    return this.http.get('/api/ordersspecyfic', ids)
        .map(res => res.json());
}

In express file I have require: multer, express,router,mongojs, db

And here is part of code in express, call to mongodb:

router.get('/ordersspecyfic', function(req, res, next){
    var ids = req.body;
    ids = ids.map(function (obj){ return mongojs.ObjectId(obj._id)});
    db.orders.find({_id: {$in: ids}}, function(err, orders){
        if(err){
            res.send(err);
        }
        res.json(orders);
    });
});

And I'm getting error:

Uncaught Response {_body: "TypeError: ids.map is not a function
  &n…/node_modules/express/lib/router/index.js:46:12)↵", status: 500, ok: false, statusText: "Internal Server Error", headers: Headers…}

Console.log in express file is showing me that req.body is an empty object {}

As far as I know req.body is not an array, but I don't know if this is only problem with that code.

All others request of get single element, get all items etc. are working fine. I just can't get this one working..

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I assume you are trying to send ids to your server side with

return this.http.get('/api/ordersspecyfic', ids)

but http.get api doesn't work like that

get(url: string, options?: RequestOptionsArgs) : Observable

In order to send this data to your back-end you should use the post api

let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });

return this.http.post('/api/ordersspecyfic', ids, options)

post(url: string, body: any, options?: RequestOptionsArgs) : Observable

Source:https://angular.io/docs/ts/latest/api/http/index/Http-class.html

over 4 years ago · Santiago Trujillo Relatório

0

Two errors, backend and frontend.

  1. Frontend error

You say this.http.get('/api/ordersspecific', ids);. This does nothing - or specifically, this only tries to get /api/ordersspecific. It doesn't send ids, your second parameter doesn't match any RequestOptions. In other words, your ids are ignored.

You'd want to append this as a query string. Check here how to add querystring parameters. But in short, it'd be something simple like:

return this.http.get('/api/ordersspecyfic?ids=<id1>&ids=<id2>...'
  1. Backend error

You're reading stuff from body. It's a GET request, there should be no body. Read this from querystring:

router.get('/ordersspecyfic', function(req, res, next){
  var ids = req.query.ids;
});
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda