Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SAVE A REVIEW TO DATABASE
- router.post('/', function(req, res, next) {
- var decoded = jwt.decode(req.query.token);
- User.findById(decoded.user._id, function(err, doc) {
- if (err) {
- return res.status(404).json({
- title: 'An error occurred',
- error: err
- });
- }
- var review = new Review({
- content: req.body.content,
- rating: req.body.rating,
- app: req.body.appId,
- user: doc
- })
- Application.findById(req.body.appId, function(err, doc) {
- if (err) {
- return res.status(404).json({
- title: 'An error occurred',
- error: err
- });
- }
- review.save(function(err, result) {
- if (err) {
- return res.status(404).json({
- title: 'An error occurred',
- error: err
- });
- }
- review.update(function(err, result) {
- if (err) {
- return res.status(404).json({
- title: 'An error occurred',
- error: err
- });
- }
- doc.reviews.push(result);
- doc.save();
- res.status(201).json({
- message: 'Saved review',
- obj: result
- });
- });
- });
- });
- Review Model:
- var schema = new Schema({
- content: {type: String, required: true},
- rating: {type: Number},
- dateCreated: {type: String},
- app: {type: Schema.Types.ObjectId, ref: 'Application'},
- user: {type: Schema.Types.ObjectId, ref: 'User'}
- });
- Application Model:
- var schema = new Schema({
- name: {type: String, required: true},
- description: {type: String},
- reviews: [{type: Schema.Types.ObjectId, ref: 'Review'}],
- })
- User Model:
- var schema = new Schema({
- firstName: {type: String, required: true},
- lastName: {type: String, required: true},
- password: {type: String, required: true},
- passwordConfirm: {type: String, required: true},
- email: {type: String, required: true, unique: true},
- reviews: [{type: Schema.Types.ObjectId, ref: 'Review'}]
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement