Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. My mongoose model is not saving on the callback and I don't know why...
  2.  
  3.  
  4. &&&Model&&&
  5.  
  6. var mongoose = require('mongoose');
  7. var Schema = mongoose.Schema;
  8.  
  9. var CommentSchema = new Schema({
  10. body: {type: String, required: true, max: 2000},
  11. created: { type: Date, default: Date.now },
  12. flags: {type: Number, default: 0}
  13. }, {
  14. writeConcern: {
  15. w: 0,
  16. j: false,
  17. wtimeout: 200
  18. }
  19. });
  20.  
  21. var PostSchema = new Schema({
  22. body: {type: String, required: true, max: 2000},
  23. created: { type: Date, default: Date.now },
  24. flags: {type: Number, default: 0},
  25. comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }]
  26. }, {
  27. writeConcern: {
  28. w: 0,
  29. j: false,
  30. wtimeout: 200
  31. }
  32. });
  33.  
  34. var Post = mongoose.model('Post', PostSchema);
  35. var Comment = mongoose.model('Comment', CommentSchema)
  36.  
  37. module.exports = {
  38. Post: Post,
  39. Comment: Comment
  40. }
  41.  
  42.  
  43. &&&route&&&
  44.  
  45. var express = require('express');
  46. var router = express.Router();
  47. const fs = require('fs');
  48. var model = require('../models/model');
  49. // var getDirName = require('path').dirname;
  50.  
  51. /* GET home page. */
  52. router.get('/', function(req, res, next) {
  53. res.render('index', { title: 'Express' });
  54. });
  55.  
  56. router.post('/uploadPost', (req, res, next)=>{
  57. console.log('inside /uploadPost')
  58. // console.log('and value of req.body: ', req);
  59. console.log('value of req.files: ', req.files)
  60. console.log('value of req.body: ', req.body)
  61.  
  62. var post = {
  63. body: req.body.post,
  64. created: Date.now(),
  65. flags: 0,
  66. comments: []
  67. }
  68.  
  69. console.log('value of post: ', post)
  70.  
  71. let postInstance = new model.Post(post)
  72.  
  73. console.log('value of postInstance: ', postInstance)
  74.  
  75. postInstance.save().then(post=>{
  76. console.log('value of post: ', post)
  77. }).catch( (e) => {
  78. console.log('There was an error', e.message);
  79. });
  80.  
  81. res.json({return: 'return from /uploadPost'})
  82. })
  83.  
  84. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement