简体   繁体   English

无法从获取请求 Node.js、Express 中获取对象

[英]Cant get a object from a get request Node.js, Express

I'm following Traversy Media's Node.js tutorial and have run into an issue i'm unable to solve.我正在关注 Traversy Media 的Node.js教程,但遇到了一个我无法解决的问题。 I'm trying to get the response of one object from an array of objects with the code:我正在尝试使用以下代码从一组对象中获取一个对象的响应:

app.get('/api/members/:id', (req, res) => {
  res.json(members.filter(member => member.id === parseInt(req.params.id)));
});

but using postman (or typing this into my browser) when I try to get http://localhost:5000/api/members/1 it gives me an empty array.但是当我尝试获取http://localhost:5000/api/members/1时使用邮递员(或在我的浏览器中输入它)它给了我一个空数组。

This is my main JavaScript file:这是我的主要 JavaScript 文件:

const express = require('express');
const path = require('path');
const app = express();
const logger = require('./middleware/logger')

const members = require('./Members')

//init middleware
//app.use(logger);

//Gets all members

app.get('/api/members', (req, res) => res.json(members));

//Get Single Members
app.get('/api/members/:id', (req, res) => {
  res.json(members.filter(member => member.id === parseInt(req.params.id)));
});
//Set a static folder
app.use(express.static(path.join(__dirname, 'public')));

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => console.log(`Server started on port ${PORT}`));

And this is the Members.js file这是Members.js 文件

const members = [
    {
      name:"john",
      email:"john@mail.com",
      status: "active"
    },
    {
      name:"sally",
      email:"sally@mail.com",
      status: "inactive"
    },
    {
      name:"josh",
      email:"josh@mail.com",
      status: "active"
    }
];

module.exports = members;

I'm totally stuck and any help is very appreciated.我完全被困住了,非常感谢任何帮助。

The objects in your array have no 'id' field, which is why nothing is being matched with the filter.数组中的对象没有“id”字段,这就是为什么没有与过滤器匹配的原因。

What you're saying is: 'Find the object in this array, which has an 'id' field of X', where X is req.params.id.你的意思是:'在这个数组中找到一个'id'字段为X的对象,其中X是req.params.id。 This is not matching anything because your objects only have 'name', 'email', and 'status' fields, ie there is no 'id' field, hence nothing is being matched when you call the filter method.这不匹配任何内容,因为您的对象只有“名称”、“电子邮件”和“状态”字段,即没有“id”字段,因此在调用过滤器方法时没有匹配任何内容。

This should become clear if you do a little test..如果你做一个小测试,这应该会变得清楚。

Replace this替换这个

app.get('/api/members/:id', (req, res) => {
  res.json(members.filter(member => member.id === parseInt(req.params.id)));
});

With this有了这个

app.get('/api/members/:email', (req, res) => {
  res.json(members.filter(member => member.email === req.params.email));
});

And when you're sending the request, send up 'josh@mail.com' as the request parameter, you should find that this record is returned:当你发送请求时,发送 'josh@mail.com' 作为请求参数,你会发现返回了这条记录:

http://localhost:5000/api/members/josh@mail.com http://localhost:5000/api/members/josh@mail.com

    {
      name:"josh",
      email:"josh@mail.com",
      status: "active"
    }

This happens because your objects have an email field, and you're matching against that, and there IS an object who's email field has a value of 'josh@mail.com'.发生这种情况是因为您的对象有一个电子邮件字段,并且您正在与之匹配,并且有一个对象的电子邮件字段的值为“josh@mail.com”。

So to reiterate, your current code is not working because you're trying to match your objects with a given 'id' field, but they do not contain an 'id' field.所以重申一下,您当前的代码不起作用,因为您试图将您的对象与给定的“id”字段匹配,但它们不包含“id”字段。

If you changed your data structure to be something like如果您将数据结构更改为类似

    {
      id: 1, // Added 'id' field
      name:"josh",
      email:"josh@mail.com",
      status: "active"
    }

Then you'd have something to match against, in that example, if you sent '1' up as the id, then your code should match that document.然后,您将有一些要匹配的内容,在该示例中,如果您将 '1' 作为 id 发送,那么您的代码应该与该文档匹配。

Hope that makes sense and is of some use.希望这是有道理的,有一定的用处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM