GET http://localhost:4000/rugs 500 (Internal Server Error)

丶灬走出姿态 提交于 2019-12-11 05:25:31

问题


I am trying to make a MEAN CRUD app. I am not exactly sure which of my routes are off, but i cannot seem to communicate with the mongodb to display data. The debugger seems to break when "rug-list.component.ts" calls the getRugs() service in "rug.service.ts". (I am also wondering: do the pathnames in the back-end files need to match those of the front-end?)

Any advice would be much appreciated. :)

» rug.service.ts (frontend)

...

@Injectable({ providedIn: "root" })
export class RugService {
    private uri = 'http://localhost:4000/rugs';

    constructor(private http: HttpClient) { }

    getRugs() {
        return this.http.get(`${this.uri}`);
    }

    getRug(id: number) {
        ...
        return this.http.get(`${this.uri}/${id}`);
    }
    ...
    deleteRug(id: number) {
        return this.http.get(`${this.uri}/${id}/delete`);
    }

   ...
}

» server.js (backend)


...

app.use('/rugs', rugRoute);

» rug.route.js (backend)

...

//list
rugRoutes.route('/').get(function (req, res) {
    find(function (err, rugs) {
        if (err) { console.log(err); }
        else { res.json(rugs); }
    });
});

//details
rugRoutes.route('/:id').get(function (req, res) {
    let id = req.params.id;
    findById(id, function (err, rug) {
        res.json(rug);
    });
});

//add
rugRoutes.route('/0/edit').post(function (req, res) {
    let rug = new Rug(req.body);
    rug.save().then(
        () => { res.status(200).json({ 'rug': 'Rug added successfully' }); })
        .catch(err => { res.status(400).send("Unable to save to database"); });
});

» app-routing.module.ts (not sure if this is necessary) (frontend)

...

const routes: Routes = [
  { path: 'rug-list', component: RugListComponent },
  { path: 'rug-list/:id', component: RugDetailComponent },
  { path: 'rug-list/:id/edit', component: RugEditComponent },
  { path: '**', component: HomeComponent, pathMatch: 'full' },
];


回答1:


Make sure the "this.http.get/put/post" in the front-end file rug.service.ts matches the "rugRoutes.route(pathname).get/put/post(...)" in the back-end file rug.route.js.



来源:https://stackoverflow.com/questions/56658124/get-http-localhost4000-rugs-500-internal-server-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!