【问题标题】:Node Js API - Creating GET requestNode Js API - 创建 GET 请求
【发布时间】:2019-06-30 07:23:44
【问题描述】:

我正在使用 nodejs 和 MongoDB 创建我的第一个 API,我想创建一个 GET 请求,例如“localhost:3050 / user / name”.. 所以我建立了这个:

server.get('/user/:name',UserController.read);

...

const Schema = mongoose.Schema;

const UserSchema = new Schema({

    name: String,
    email: String,
    compagny: String,


},{collection: "USER_COLLEC"});

const User = mongoose.model('user',UserSchema);

...

read (req,res) {
        const name= req.body.name;
        User.find({name : name}).then( (user) => {
            res.send({user});
        })
    }

此代码返回我:

{
    "user": []
}

【问题讨论】:

  • 什么是User?我猜这是某种数据库API。该 API 的文档对搜索其他字段有何说明?
  • 我重新编辑了我的问题

标签: javascript node.js json


【解决方案1】:

当您使用路由参数时,req.params 将在匹配的路由中返回参数 使用const name=req.params.name 而不是const name= req.body.name;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2020-01-10
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    相关资源
    最近更新 更多