-2

I keep getting the aforementioned error when I try to do a PUT request to update (error occurs at the line "item.save(function (err) ..." . Below is my PUT request code:

outer.put('/alltweets/:id', function(req, res){
    var db = req.db; 
    db.collection('tweetdb').findById(req.params.id, function (err, item){
        if (err) 
            res.send(err); 
        item.tweet = req.body.tweet; 
        item.date = req.body.date; 

        //save the item 
        item.save(function(err) {
            if(err) 
                res.send(err); 

            res.json({message: 'item updated' }); 
        }); 
    });     
}); 

Currently using Node, Express, and MongoDB.


The easiest way to find a previous line, is to just store the line each time it didn't match:

with open('Perfix/prefix.txt') as f:
    previous = None
    for line in f:
        line = line.strip()
        if line == '100':
            print previous
        previous = line

Note that you do not need to read all lines into memory first; just loop over the file object.

The next() function is not something you should be using here at all; it requires an iterator and would advance it to the next item, not rewind it to a previous entry.

4

1 に答える 1

0

いくつかの調査を行い、最終的に save メソッドが Mongoskin には実際には存在しないことに気付きました。代わりに更新 (特に新しいバージョンでは updateById) を使用します。PUT リクエストが以下のコードで機能するようになりました (Mongoskin の場合):

router.put('/alltweets/:id', function(req, res){
    var db = req.db; 
    db.collection('tweetdb').updateById(req.params.id, {$set:req.body}, {safe: true, multi: false}, function(e, result){

    if (e) return next (e); 
    res.send((result===1)?{msg:'success'}:{msg:'error'})

    }); 
}); 
于 2014-10-25T14:10:31.593 に答える