如何使用 nodejs、expressjs 在 remote-mongodb 中远程创建 collection 形式的 mlab api?
How create collection remotely form mlab api in remote-mongodb using nodejs,expressjs?
我正在关注 Brad Traversy's MERN 堆栈开发课程,在第 4 部分中,我无法向路由 localhost:5000/api/profile
、
发出 post
请求
在发送 post request
数据 handle,status,skills
后,这是我 collection 中的字段,它 returns 错误 skills field required
.
skills 是从 user-input.
发送的字符串数组
当我检查 collection-profile
是否创建时,它没有创建,只有一个 collection 显示为主要 collection user
、
我按照教程中的每一行代码进行操作但出现错误,我想在远程 mlab mongodb、
上创建 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
模块,我正在验证 input-fields,
if (Validator.isEmpty(data.skills)) {
errors.skills = "Skills field is required";
}
所以,我不明白为什么我找不到新的 collection 作为配置文件?
我尝试使用 mongodb-locally,但没有用。
我的Githubrepo link.
我在路由 /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
});
}
);
这是我的提交历史,
然后它没有报错 skills field is required
,
我正在关注 Brad Traversy's MERN 堆栈开发课程,在第 4 部分中,我无法向路由 localhost:5000/api/profile
、
post
请求
在发送 post request
数据 handle,status,skills
后,这是我 collection 中的字段,它 returns 错误 skills field required
.
skills 是从 user-input.
发送的字符串数组当我检查 collection-profile
是否创建时,它没有创建,只有一个 collection 显示为主要 collection user
、
我按照教程中的每一行代码进行操作但出现错误,我想在远程 mlab mongodb、
上创建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
模块,我正在验证 input-fields,
if (Validator.isEmpty(data.skills)) {
errors.skills = "Skills field is required";
}
所以,我不明白为什么我找不到新的 collection 作为配置文件?
我尝试使用 mongodb-locally,但没有用。
我的Githubrepo link.
我在路由 /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
});
}
);
这是我的提交历史,
然后它没有报错 skills field is required
,