簡體   English   中英

用貓鼬更新文檔

[英]Updating a document with mongoose

我有一個小問題。 通過此代碼,我使用node.js和mongoose更新了mongodb中的現有文檔

router.route('/profile/:profile_id/:session_key')
    .put(function(req, res) {
        if (req._body == true && req.is('application/json') == 'application/json' ) {
            // Get the profile to update
            Profile.findOne({ profile_id: req.params.profile_id }, function(err, t_profile) {
                if (err) {
                    res.send(err);
                } else {
                    if(!t_profile) {
                        res.json({ status: 'CANT FIND PROFILE TO UPDATE' });
                    } else {
                        if (t_profile.session_key == req.params.session_key) {
                            // Update profile with new data
                            t_profile.name      = setReq( t_profile.name,  req.body.name );
                            t_profile.sex       = setReq( t_profile.sex,  req.body.sex );
                            t_profile.birthdate = setReq( t_profile.birthdate, req.body.birthdate );
                            t_profile.country   = setReq( t_profile.country,  req.body.country );
                            t_profile.password  = setReq( t_profile.password,  req.body.password );
                            // save updatd profile
                            t_profile.save( { _id: profile._id }, function(save_err, u_profile) {
                                if (save_err) {
                                    res.json({ status: 'DB ERROR' });
                                } else {
                                    res.json({ status: 'OK' });
                                }
                            });
                        } else {
                            res.json({ status: 'NOT LOGGED IN' });
                        }
                    //res.json({ status: "THIS RUNS"}); 
                    }
                }
            });
        } else {
            res.json({ status: 'ERROR', msg: 'Not application/json type'});
        }
    });

但是我的函數作為參數插入了.save()函數中,因此需要運行,我需要取消注釋res.json({status:“ THIS RUNS”})行; 以便將som響應返回給客戶端。 為什么不是res.json({status:'DB ERROR'}); 或res.json({status:'OK'}); 送回?

愚蠢的錯誤,t_profile.save(function(save_err,u_profile){...}修復了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM