代码之家  ›  专栏  ›  技术社区  ›  GD- Ganesh Deshmukh

如何使用nodejs、expressjs在远程mongodb中从mlab api远程创建集合?

  •  0
  • GD- Ganesh Deshmukh  · 技术社区  · 8 年前

    我在跟踪 Brad Traversy's post 请求路由 localhost:5000/api/profile

    发送后 post request 数据为 handle,status,skills 如果是我的集合中的字段,则返回error skills field required .

    screenshot

    skills是从用户输入发送的字符串数组。

    当我检查的时候 collection-profile 则不创建,并且只有一个集合显示为主集合 user ,

    screenshot

    我遵循教程中的每一行代码,但遇到错误,我想 Profile collection

    我的 profile 型号代码为

    const mongoose = require("mongoose");
    const Schema = mongoose.Schema;
    
    // Create Schema
    const ProfileSchema = new Schema({
      user: {
        type: Schema.Types.ObjectId,
        ref: "users"
      },
      handle: {
        type: String,
        required: true,
        max: 40
      },
      company: {
        type: String
      },
      website: {
        type: String
      },
      location: {
        type: String
      },
      status: {
        type: String,
        required: true
      },
      skills: {
        type: [String],
        required: true
      },
      bio: {
        type: String
      },
      githubusername: {
        type: String
      },
      experience: [
        {
          title: {
            type: String,
            required: true
          },
          company: {
            type: String,
            required: true
          },
          location: {
            type: String
          },
          from: {
            type: Date,
            required: true
          },
          to: {
            type: Date
          },
          current: {
            type: Boolean,
            default: false
          },
          description: {
            type: String
          }
        }
      ],
      education: [
        {
          school: {
            type: String,
            required: true
          },
          degree: {
            type: String,
            required: true
          },
          fieldofstudy: {
            type: String,
            required: true
          },
          from: {
            type: Date,
            required: true
          },
          to: {
            type: Date
          },
          current: {
            type: Boolean,
            default: false
          },
          description: {
            type: String
          }
        }
      ],
      social: {
        youtube: {
          type: String
        },
        twitter: {
          type: String
        },
        facebook: {
          type: String
        },
        linkedin: {
          type: String
        },
        instagram: {
          type: String
        }
      },
      date: {
        type: Date,
        default: Date.now
      }
    });
    
    module.exports = Profile = mongoose.model("profile", ProfileSchema);
    

    validator.js 模块,我正在验证输入字段,

      if (Validator.isEmpty(data.skills)) {
        errors.skills = "Skills field is required";
      }
    

    我试着在本地使用mongodb,但没用。

    repo link .

    1 回复  |  直到 8 年前
        1
  •  0
  •   GD- Ganesh Deshmukh    8 年前

    我返回用户的详细信息时,路线 /api/users/current 改变了 response on success

    res.json({ msg: "Success" });
    

    之后:

      passport.authenticate("jwt", { session: false }),
      (req, res) => {
    
        res.json({
          id: req.user.id,
          name: req.user.name,
          email: req.user.email
        });
      }
    );
    

    my comment

    然后它没有给出错误 skills field is required

    enter image description here