0
我目前被困在如何分配給用戶一個帖子,當他們在網站中創建時。所以基本上我想有一個登錄用戶創建一個簡歷,然後將該簡歷分配給該登錄用戶,作爲模式中的created_by。我有點分類,但我不知道要創建簡歷並將登錄用戶分配給它。如何爲用戶分配一個帖子與貓鼬和快遞
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;
var moment = require('moment');
var id = require('../helpers/generator');
let beautifyUnique = require('mongoose-beautiful-unique-validation');
const ResumeSchema = new Schema({
_id: {
type: String,
default: id.generate()
},
title: {
type: String,
required: true
},
creator: {
type: ObjectId,
ref: "User"
},
description: {
type: String,
default: "No description provided."
},
company_reviews: {
name: {
type: String,
default: "RSW"
},
review: {
type: String,
default: "asodsodo"
},
rating: {
type: String,
default: "10/10"
}
},
employee_reviews: {
name: {
type: String,
default: "Doge Dog"
},
company: {
type: String,
default: "RSW"
},
review: {
type: String,
default: "blah blah"
},
rating: {
type: String,
default: "10/10"
}
},
createdAt: {
type: String,
default: moment(new Date()).format("MMM DD, YYYY") // "Sun, 3PM"
}
});
var Resume = mongoose.model('Resume', ResumeSchema);
module.exports = Resume;
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var moment = require('moment');
var bcrypt = require('bcrypt');
var id = require('../../helpers/generator');
let beautifyUnique = require('mongoose-beautiful-unique-validation');
const UserSchema = new Schema({
_id: {
type: String,
default: id.generate()
},
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
accountType: {
type: String,
enum: ['Alphaneer', 'Administrator', 'Support', 'PRO'],
default: 'Alphaneer'
},
email: {
type: String,
required: true,
trim: true
},
username: {
type: String,
required: true,
trim: true,
unique: true
},
bio: {
type: String,
default: "No bio provided."
},
password: {
type: String,
required: true
},
createdAt: {
type: String,
default: moment(new Date()).format("MMM DD, YYYY") // "Sun, 3PM 17"
}
});
// authenticate input against content from database. BLOODY HELL ITS A MESS
UserSchema.statics.authenticate = function(email, password, callback) {
User.findOne({
email: email
})
.exec(function(err, user) {
if (err) {
return callback(err);
}
bcrypt.compare(password, user.password, function(err, result) {
if (result === true) {
return callback(null, user)
} else {
return callback();
}
})
})
}
UserSchema.pre('save', function(next) {
var user = this;
bcrypt.hash(user.password, 10, function(err, hash) {
if (err) throw err;
user.password = hash;
console.log("LOG: Password hashed & user saved.");
next();
});
});
var User = mongoose.model('User', UserSchema);
module.exports = User;
我想創建一個恢復爲登錄用戶在這裏
// POST /dashboard/resume/create
router.post('/resume/create', (req, res, next) => {
Resume.create(req.body, (err, resume) => {
if (err) {
var err = new Error("Error:" + err);
err.status = 404;
next(err);
} else {
return res.redirect('/dashboard');
}
})
});
我還創建一份簡歷時,我註冊一個用戶。的歡迎東西類
// POST /register
router.post('/', (req, res, next) => {
var data = {
firstName: req.body.fName,
lastName: req.body.lName,
username: req.body.username,
email: req.body.email,
password: req.body.password,
bio: req.body.bio
};
User.create(data, (err, user) => {
if (err) {
throw err;
}
var resume = {
title: "Welcome to NAME!"
}
Resume.create(resume, (err, resume) => {
if (err) throw err;
return res.redirect('/');
});
});
});
任何幫助表示讚賞,我也可以做一些進一步的解釋,如果它不決策意識:)
好吧,所以req.user是我登錄的用戶原因使用會話?這會適合在這裏,還是應該堅持req.user? – Ethan
我的意思是你應該將登錄的用戶資源保存在某個地方。會話也會這樣做,或者如果你想要的話,你可以查看JWT(json web令牌)以獲得基於令牌的認證。使用用戶對象簽署令牌,並在用戶使用令牌發出api請求時,在req.user中提取這些詳細信息。當請求url中有userId時,另一種方法是使用'req.params'並在userId在params中時加載用戶對象。 – Gaurav
好的,我正在使用JWT,所以我可能會這麼做。謝謝! – Ethan