gpt4 book ai didi

mongodb - 递增对象不起作用( mongoose、node.js、express、mongodb )

转载 作者:太空宇宙 更新时间:2023-11-04 02:44:39 26 4
gpt4 key购买 nike

我提交了这个应用程序 here

基本问题是,在 server.js @ line 234 中,我有一个方法可以增加参数对象中的计数器。这不起作用 - 您可以在 models.js 中查找适当的数据模型。

在第 242 行,我对枢轴对象中的计数器进行了另一个增量,该对象是参数对象中的文档数组。在这里,设置相同的计数器工作 - 我不确定我在这里做错了什么。

编辑:添加来自 github 的代码

数据模型

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);

路由预参数条件

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();
});

重定向

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);
});

一些调试

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 } }

在上述参数的枢轴“gmail”上运行console.dir(req.pivot.counter)会返回。在这种情况下,计数器递增并成功显示

{ _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 } }

最佳答案

已确认,Mongoose v1.3.5 再次正常工作 - 代码没问题,Mongoose 有一个错误。

结果是 Mongoose v.1.3.1 - 1.3.3 中的一个错误。它已在最近的提交中修复,但尚未在主构建中

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

关于mongodb - 递增对象不起作用( mongoose、node.js、express、mongodb ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6004631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com