[英]PouchDB 409 conflict when adding doc.
I am getting a strange conflict with pouchDB when adding a new document. 添加新文档时,我与pouchDB发生了奇怪的冲突。 The conflict arises when creating a new doc shortly after another is made.
创建新文档后不久就会产生冲突。 However if I wait ~5 minutes or so the conflict no longer occurs.
但是,如果我等待约5分钟左右,则不再发生冲突。 I am very stuck with solving this.
我非常坚持解决这个问题。
I am using a express with node. 我在节点上使用快递。 Here is my method for adding an item.
这是我添加项目的方法。 It is used in a router (see below)
用于路由器(请参阅下文)
//require stuff
...
//require pouch
var PouchDB = require('pouchdb');
//db setup
var db = new PouchDB('http://127.0.0.1:5984/db');
module.exports = {
addItem: (req, res, next) => {
//check body fields with express validator
req.checkBody('firstname', "Invalid, please enter firstname").notEmpty();
req.checkBody('lastname', "Invalid, please enter lastname").notEmpty();
req.checkBody('address', "Invalid, please enter address").notEmpty();
var phonenumber = req.body.phonenumber; //phone number is optional dont use express validator
var errors = req.validationErrors();
if (errors) {
res.render('add', {
errors: errors
});
}
var newDoc = {
_id: date.toString() ,
firstname: req.body.firstname,
lastname: req.body.lastname,
address: req.body.address,
phonenumber: phonenumber,
dateAdded: moment(date).format("dddd, MMMM Do YYYY, h:mm:ss a")
}
db.put(
newDoc
).then(function (response) {
res.redirect('/feed');
}).catch(function (err) {
if (err){
return next(err);
}
});
},
...
//more methods
}
...
//export module
router for adding 路由器添加
//require all the stuff
//post new item route
router.route('/feed/add').post(ActionController.addItem);
I used the pouchdb-upsert to fix this. 我使用了pouchdb-upsert来解决此问题。 https://github.com/pouchdb/upsert
https://github.com/pouchdb/upsert
and used the puIfNotExists method. 并使用了puIfNotExists方法。
...
//save item to array and db
db.putIfNotExists({
_id: date.toString(),
firstname: req.body.firstname,
lastname: req.body.lastname,
address: req.body.address,
phonenumber: phonenumber,
dateAdded: moment(date).format("dddd, MMMM Do YYYY, h:mm:ss a")
}).then(function () {
return res.redirect('/feed');
}).catch(function (err) {
if (err) {
return next(err);
}
})
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.