Как создать коллекцию удаленно с помощью mlab api в remote-mongodb, используя nodejs, expressjs? - PullRequest
0 голосов
/ 05 сентября 2018

Я следую Курс по разработке стека Брэда Траверси MERN и в 4-м разделе я не могу сделать post запрос на маршрутизацию localhost:5000/api/profile,

после отправки post request с данными handle,status,skills, которые являются полями в моей коллекции, возвращается ошибка skills field required.

screenshot

skill - это массив строк, отправленных с пользовательского ввода.

когда я проверял, collection-profile создан или нет, он не создается, и в качестве основной коллекции отображается только одна коллекция user,

screenshot

Я следовал за каждой строкой кода в учебнике, но получая ошибку, я хотел получить Profile collection для создания на удаленном mlab mongodb,

мой 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 локально, но это не помогло.

my Github ссылка на репо .

1 Ответ

0 голосов
/ 05 сентября 2018

Я вернул данные пользователя при маршруте /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

...