node.js自动创建页面

时间:2017-10-15 18:14:41

标签: javascript node.js express forum

我正在建立一个快速论坛网站。而且我被困住了。我无法弄清楚,当用户创建讨论时,必须自动创建一个新的讨论页面。我不知道它是如何工作的。即使我真的需要为每个讨论创建一个新页面。

----------这是网页用户创建讨论---------

form.discussionForm(method="post" action="/profile" encType="multipart/form-data")
      fieldset
        legend START A DISCUSSION
          .errorMsg=profileError
        label(for="one") * question
        input(type="text" name="question" id="one")
        label(for="two") details
        textarea(name="details" id="two")
        .upload-btn-wrapper
          label.btn(for="three") Upload image
          input(type='file' name='image' id="three")
        button(type="submit") post

-------这是我处理讨论数据的后路线--------

router.post(' / profile',function(req,res){

if (req.body.question && req.files.image) {

    var imgNewPath = function imgNewPath() {
        return req.session.userId + Math.floor(Math.random() * 99999999999999 + 1);
    };

    var thePath = imgNewPath();
    //express file uploader req.files...name
    var userImg = req.files.image;

    userImg.mv("public/img/profileImages/" + thePath, function (err) {
        if (err) {
            console.log("error with discussion image");
        }

        User.findByIdAndUpdate(req.session.userId, { $push: { "discussions": { title: req.body.question, details: req.body.details, imgPath: thePath } } }, { safe: true, upsert: true }, function (err, model) {
            if (err) {
                console.log("error with saving discussion");
            }

            res.redirect('/profile');
        });
    });
} else if (req.body.question) {

    User.findByIdAndUpdate(req.session.userId, { $push: { "discussions": { title: req.body.question, details: req.body.details } } }, { safe: true, upsert: true }, function (err, model) {
        if (err) {
            console.log("error with saving discussion details and question");
        }

        res.redirect('/profile');
    });
} else {
    User.findById(req.session.userId).exec(function (err, user) {
        if (err) {
            if (err) {
                console.log("error with  finding user id");
            }
        } else {

            return res.render('profile', { thisUser: user, profileError: '*Ask a question' });
        }
    });
}

});

--------这是我在哪里列出的热门讨论--------

'使用严格的';

router.get(' /',function(req,res){

            //we find all user info
            User.find({}, function (err, result) {

                            //we loop thru all users
                            for (var i = 0; i < result.length; i++) {

                                            //this foo get the imgPath and push the whole information into the rawArray
                                            var pusher = function pusher(a, b, c) {
                                                            var img = result[i].profilePicture;
                                                            rawArray.push([img, a, b, c]);
                                            };

                                            //we are looping through disscussions of the looping user   


                                            for (var x = 0; x < result[i].discussions.length; x++) {
                                                            var title = result[i].discussions[x].title,
                                                                votesUp = result[i].discussions[x].votesUp,
                                                                votesDown = result[i].discussions[x].votesDown;
                                                            pusher(title, votesUp, votesDown);
                                            }

                                            //when loop finish we take action, because js is assnycroniset so we have to use this trigger
                                            if (i === result.length - 1) {
                                                            res.render('index', { data: readyArray().slice(0, 10), dataScroll: readyArray().slice(10, 20) });
                                            }
                            }
            });

});

----这是我在哪里列出的热门讨论--------

      ul
          each val in data
            li
              img(src=`${val[0]}`)
              a(href="XXXXXXXXXXXXXXXX")
                h2 #{val[1]} &nbsp;)

-------------

我试图找出如何为每个讨论创建讨论页面。并将它们链接到主页。你在哪里看到XXXXXXXXX

1 个答案:

答案 0 :(得分:0)

好的,所以对我来说,为什么你要将新的论坛讨论发布到当前用户的个人资料中是没有意义的......

也就是说,任何创建,读取,更新,删除周期(无论它是什么)的一般模式都是这样的:

const router = new express.Router();

// if there's a param 'discussion_id', the passed function will run
router.param('discussion_id', (req, res, next, id) => {
  // assuming you're using mongoose
  Discussion.findById(id, (err, discussion) => {
    if (err) return next(err);
    req.discussion = discussion;
    return next();
  });
});

router.route('/discussions/:discussion_id')
   .get((req, res, next) => {
       // if this is populated, you have a single discussion
       if (req.discussion) return res.render('discussion', discussion);
       // otherwise, return a list. probably want to limit to 10 or something
       Discussion.find({}, (err, discussions) => { 
          if (err) return next(err);
          return res.render('index', discussions);
       });
    })
    .put((req, res, next) => {
       // you'll have access to req.discussion here, so you can update and save it
    })
    .delete((req, res, next) => {
       // you'll have access to req.discussion here too, so again you can use that to delete it
    })
    .post((req, res, next) => {
        // create a new discussion
    });