湂劊: express坺獙,mongodb中桝加莰录,ä¸?䟚进兼saveĺ›žč°ƒ.


ćœ€čż‘ĺœ¨ĺ­Śexpress坺獙,写了一个甾役网獙的demo,é ‡ĺˆ°ä¸€ç‚šé—Žé˘˜,癞ć€?ä¸?垗兜解,ćą‚ĺ¤§çĽžćŒ‡ç‚š.

é—Žé˘˜ć??čż°:
ĺœ¨ć–°ĺ˘žčŽ°ĺ˝•çš„ć—śĺ€™,襨ĺ?•ĺĄŤĺ†™ĺŽŒćˆ?炚凝察兼,诡湂ĺ?‘é€ ĺ‡şĺŽťäş†,ä˝†ć˜ŻĺŽƒäźšä¸€ç›´čŻˇćą‚,ä¸?䟚进兼saveĺ›žč°ƒ,äšŸć˛Ąćœ‰ćŠĽé”™.(čż™é‡Œčż›çš„ć˜Żif中elseéƒ¨ĺˆ†çš„äťŁç  ĺ?—)


 app.post('/admin/movie/new', function (res, req) {
    var id = res.body.movie._id;
    var movieObj = res.body.movie;
    var _movie;
    if (id !== 'undefined') {
        Movie.findById(id, function (err, movie) {
            if (err) {
                console.log(err);
            }

            _movie = _.extend(movie, movieObj);
            _movie.save(function (err, movie) {
                if (err) {
                    console.log(err);
                }

                res.redirect('/movie/' + movie._id);
            });
        });
    } else {
        _movie = new Movie({
            doctor: movieObj.doctor,
            title: movieObj.title,
            country: movieObj.country,
            language: movieObj.language,
            year: movieObj.year,
            poster: movieObj.poster,
            summary: movieObj.summary,
            flash: movieObj.flash
        });

        _movie.save(function (err, movie) {
            if (err) {
                console.log(err);
            }

            res.redirect('/movie/' + movie._id);
        });
    }
});

éĄšç›Žĺœ°ĺ?€: https://github.com/xjchenhao/Learn/tree/master/nodejs/%E7%AB%99%E7%82%B9%E5%BB%BA%E8%AE%BE/imooc

回片ć??čż°

Express node

传说D裤子 9 years, 10 months ago

ä˝ çš„ new Movie äťŁç  ćœ€ĺĽ˝ĺˆ—ĺ‡şäťĽä¸‹ďźŒĺ› ä¸şć˜Żĺź‚ć­Ľ 所䝼你的 Movie 返回的 model ä¸?ä¸€ĺŽšĺ˝“ć—śĺ­˜ĺœ¨ă€‚ 祎äż?用promise樥ĺź?。

所䝼saveä¸?ĺ­˜ĺœ¨äš…čˇłčż‡äş†ďźŒä¸?䟚进兼。

DJLubel answered 9 years, 10 months ago

ć˜Ż res.redirect('/movie/' + movie._id); 写错了,ĺş”čŻĽć˜Żreq.. ä¸?弽ć„?ć€?đŸ˜Œ

ariahsh answered 9 years, 10 months ago

Your Answer