0

I am developing a web app using the MEAN stack (no Mongo for now) I am trying to pass the name of a file on the server using a query paramerer, the error happens when i get :

"localhost:8080/api/result?filename=for-debug-file-name"

It is working well if I remove the console.log() right below But when I get the query parameter it gets me the "Error: No default engine was specified and no extension was provided”.

(This route correspond to api/result)

var express = require('express');
var router = express.Router();   

router.get('/', function(req, res) {
    console.log(req.query('filename')); // ERROR
    res.status(200).json({ "json-test": 42 });
})

module.exports = router;

Here are my angular routes :

const appRoutes: Routes = [
{
    path: 'result',
    component: ResultComponent,
},
{
    path: 'upload',
    component: UploaderComponent,
},
{
    path: '',
    redirectTo: '/upload',
    pathMatch: 'full'
}];

And here is my ResultComponent.ts :

ngOnInit() {
    this.getParsedDocumentData('for-debug-file-name');
}

getParsedDocumentData(fileName: string): Observable<string[]> {
    let params = new URLSearchParams();
    params.append('filename', fileName);
    let options = new RequestOptions({ params: params });

    return this.http.get('http://localhost:8080/api/result/', options)
                    .map(res => res.json())
                    .catch(this.handleError);
}

private handleError (error: any) {
    return Observable.throw(error);
}

I would really appreciate your help as I have been stuck for 4 hours. Thanks.

Banana
  • 2,435
  • 7
  • 34
  • 60
  • 1
    It's unclear why you get this message because it usually appears when you do res.render(), i.e. actually use template engine. But I'm not sure why you use query as a function. It should work as req.query.filename. It is possible that req.query() causes an error, and default error handler tries to output error page with res.render(). – Estus Flask Feb 16 '18 at 11:28
  • Actually using the dot notation returns undefined and i dont know why – Hugo Beisser Feb 16 '18 at 11:56
  • have you connect ```express.Router``` file to ```app``` ? like ```app.use(router-file-required);``` – Lalit Goswami Feb 16 '18 at 12:24
  • yes i unfortunately did, actually if I put the string name of the file instead of the query parameters, it works fine – Hugo Beisser Feb 16 '18 at 12:40
  • Well, calling req.query as function when it's not a one definitely won't help. – Estus Flask Feb 16 '18 at 21:22
  • Possible duplicate of [How to access the GET parameters after "?" in Express?](https://stackoverflow.com/questions/17007997/how-to-access-the-get-parameters-after-in-express) – Estus Flask Feb 16 '18 at 21:22

1 Answers1

0

query method in request object does not exists. Instead use query property to access filename parameter.

console.log(req.query.filename);

Reference

explorer
  • 944
  • 8
  • 18