简体   繁体   English

增量对象不起作用(mongoose,node.js,express,mongodb)

[英]Incrementing Object not working ( mongoose, node.js, express, mongodb )

I have a commit of this app up here 在这里有这个应用程序的提交

The basic problem is, in server.js @ line 234 I have a method that increments the counter in the param oject. 基本问题是,在server.js @第234行中,我有一种方法可以增加参数对象中的计数器。 This does not work - you can look in models.js for the appropriate datamodels. 这不起作用-您可以在models.js中查找适当的数据模型。

At line 242, I have another increment to a counter in the pivot object which is an array of docs within a param object. 在第242行,我在数据透视对象中的计数器上有了另一个增量,该对象是param对象内的一系列文档。 Here, the counter which is set up identically works - I'm not sure what I'm doing wrong here. 在这里,设置相同的计数器可以工作-我不确定我在做什么错。

EDIT: Added code from github 编辑:从github添加代码

The data models 数据模型

var Pivot = new Schema({
    value       : {type: String, validate: [validateLength, 'length error'] } 
  , destination : {type: String, validate: [validateUrl, 'url error'] } 
  , counter     : {type: Number, default: 0 }
 });



var Param = new Schema({
    title      : {type: String, validate: [validateLength, 'length error'] } 
  , desc       : {type: String, validate: [validateDesc, 'length error'] }
  , defaultUrl : {type: String, validate: [validateUrl, 'url error']  } 
  , counter    : {type: Number, default: 0 }
  , pivots     : [Pivot]
});


mongoose.model('Param', Param);

The Route Pre-Param conditions 路线预参数条件

app.param('title', function(req,res, next){
    Param.findOne({"title":req.param('title')}, function(err, record){
        if (err) return next(err);
        if (!record) return next (new Error('Parameter Not Found') ); 
        req.record = record;
        next();
    });         
}); 


app.param('value', function(req,res, next){
        req.pivot = req.record.findPivot(req);
        if (!req.pivot) return next (new Error('Pivot Not Found') ); 
        next();
}); 

The Redirects 重定向

app.get('/redirect/:title', function(req, res, next){
    req.record.counter++;
    req.record.save();
    res.redirect(req.record.defaultUrl);      
});


app.get('/redirect/:title/:value', function(req, res, next){
    req.pivot.counter++;
    req.record.save();
    res.redirect(req.pivot.destination);
});

Some Debugging 一些调试

console.dir(req.record.counter) Seems to output the parent object, and counter shows up as [Circular]. console.dir(req.record.counter)似乎输出父对象,并且计数器显示为[Circular]。

{ _atomics: {},
  _path: 'counter',
  _parent: 
   { doc: 
      { counter: [Circular],
        pivots: [Object],
        _id: 4dce2a3399107a8a2100000c,
        title: 'varun',
        desc: 'my blog',
        defaultUrl: 'http://varunsrin.posterous.com/' },
     activePaths: 
      { paths: [Object],
        states: [Object],
        stateNames: [Object],
        map: [Function] },
     saveError: null,
     isNew: false,
     pres: { save: [Object] },
     errors: undefined } }

Running console.dir(req.pivot.counter) on a pivot 'gmail' of the param above returns. 返回上面参数的枢轴“ gmail”上运行console.dir(req.pivot.counter)。 In this case, the counter increments and displays successfully 在这种情况下,计数器增加并成功显示

{ _atomics: {},
  _path: 'counter',
  _parent: 
   { parentArray: 
      [ [Circular],
        _atomics: [],
        validators: [],
        _path: 'pivots',
        _parent: [Object],
        _schema: [Object] ],
     parent: undefined,
     doc: 
      { counter: [Circular],
        _id: 4dce2a6499107a8a21000011,
        value: 'gmail',
        destination: 'http://www.gmail.com/' },
     activePaths: 
      { paths: [Object],
        states: [Object],
        stateNames: [Object] },
     saveError: null,
     isNew: false,
     pres: { save: [Object] },
     errors: undefined } }

Confirmed, this is working again with Mongoose v1.3.5 - the code was fine, Mongoose had a bug. 确认,这可以再次用于Mongoose v1.3.5-代码很好,Mongoose有一个错误。

Turned out to be a bug in Mongoose v.1.3.1 - 1.3.3. 原来是Mongoose v.1.3.1-1.3.3中的错误。 It has been fixed in a recent commit, but isnt in the main build yet 它已在最近的提交中修复,但尚未在主版本中修复

https://github.com/LearnBoost/mongoose/issues/342 https://github.com/LearnBoost/mongoose/issues/342

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM