MongoDB: 正确保存数据到模型
MongoDB: Saving Data to Model Correctly
目标:用户模型需要为用户关联工作数据。我需要能够像这样使用 EJS 访问用户的工作数据:<%= user.jobs[0] %>
当前用户模型:
var UserSchema = new mongoose.Schema({
username: String,
password: String,
jobs :[{
name: String,
score: String
}]
});
用户名和密码工作正常。但是我需要让工作数据正常工作。工作数据是一个前端 javascript 数组,包含 200 个特定于用户的排名工作。
我可以像这样访问这个变量中的数据:
$( "#testButton").click(function() {
fetch('/test', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(jobs)
}).then((response) => {
console.log(response)
}).catch((err) => {
console.log(err);
})
这给我的数据看起来像这样...
[{
name: 'Administrative assistant',
score: -48 },
{
name: 'Adult literacy instructor',
score: 28 },
{
name: 'Advertising copywriter',
score: 45
}]
我的问题是......我如何获取这些数据......并将其正确地放入用户模型中?注意:我正在使用 body-parser.
我的 POST 路线示例...
router.post("/register", function(req, res){
var newUser = new User({username: req.body.username});
User.register(newUser, req.body.password, function(err, user){
if(err){
.... etc.
models/User.js
const mongoose = require("mongoose");
const schema = new mongoose.Schema({
username: String,
password: String,
jobs: [
{
name: String,
score: String,
},
],
});
module.exports = mongoose.model("user", schema);
index.js
const app = require("express")();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const User = require("./models/User");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/users", async (req, res) => {
const user = await User.create({
username: req.body.username,
password: req.body.password,
jobs: req.body.jobs,
});
return res.status(201).json({
message: "Created",
data: user,
});
});
mongoose.connect("mongodb://root:toor@0.0.0.0:27017/Whosebug");
mongoose.connection.on("error", err => console.error(err));
app.listen(3000, () => console.log("Listening on :3000"));
使用 /users
的数据发出 post
请求:
{
"username": "jon",
"password": "snow",
"jobs": [
{
"name": "software dev", "score": "10 pts"
},
{
"name": "frontend dev", "score": "8 pts"
}
]
}
将return:
{
"message": "Created",
"data": {
"_id": "5d86ec91023b9785f2b455bb",
"username": "jon",
"password": "snow",
"jobs": [
{
"_id": "5d86ec91023b9785f2b455bd",
"name": "software dev",
"score": "10 pts"
},
{
"_id": "5d86ec91023b9785f2b455bc",
"name": "frontend dev",
"score": "8 pts"
}
],
"__v": 0
}
}
目标:用户模型需要为用户关联工作数据。我需要能够像这样使用 EJS 访问用户的工作数据:<%= user.jobs[0] %>
当前用户模型:
var UserSchema = new mongoose.Schema({
username: String,
password: String,
jobs :[{
name: String,
score: String
}]
});
用户名和密码工作正常。但是我需要让工作数据正常工作。工作数据是一个前端 javascript 数组,包含 200 个特定于用户的排名工作。
我可以像这样访问这个变量中的数据:
$( "#testButton").click(function() {
fetch('/test', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(jobs)
}).then((response) => {
console.log(response)
}).catch((err) => {
console.log(err);
})
这给我的数据看起来像这样...
[{
name: 'Administrative assistant',
score: -48 },
{
name: 'Adult literacy instructor',
score: 28 },
{
name: 'Advertising copywriter',
score: 45
}]
我的问题是......我如何获取这些数据......并将其正确地放入用户模型中?注意:我正在使用 body-parser.
我的 POST 路线示例...
router.post("/register", function(req, res){
var newUser = new User({username: req.body.username});
User.register(newUser, req.body.password, function(err, user){
if(err){
.... etc.
models/User.js
const mongoose = require("mongoose");
const schema = new mongoose.Schema({
username: String,
password: String,
jobs: [
{
name: String,
score: String,
},
],
});
module.exports = mongoose.model("user", schema);
index.js
const app = require("express")();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const User = require("./models/User");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/users", async (req, res) => {
const user = await User.create({
username: req.body.username,
password: req.body.password,
jobs: req.body.jobs,
});
return res.status(201).json({
message: "Created",
data: user,
});
});
mongoose.connect("mongodb://root:toor@0.0.0.0:27017/Whosebug");
mongoose.connection.on("error", err => console.error(err));
app.listen(3000, () => console.log("Listening on :3000"));
使用 /users
的数据发出 post
请求:
{
"username": "jon",
"password": "snow",
"jobs": [
{
"name": "software dev", "score": "10 pts"
},
{
"name": "frontend dev", "score": "8 pts"
}
]
}
将return:
{
"message": "Created",
"data": {
"_id": "5d86ec91023b9785f2b455bb",
"username": "jon",
"password": "snow",
"jobs": [
{
"_id": "5d86ec91023b9785f2b455bd",
"name": "software dev",
"score": "10 pts"
},
{
"_id": "5d86ec91023b9785f2b455bc",
"name": "frontend dev",
"score": "8 pts"
}
],
"__v": 0
}
}