Note that there are some explanatory texts on larger screens.

plurals
  1. PODuplicate DB submissions with mongoose
    primarykey
    data
    text
    <p>I'm new to node.js and I'm having an intermittent issue with some commands being duplicated. I think it might have something to do with the fact that I'm trying to separate my db interaction from the rest of my code and I haven't isolated it properly. I have a file called transaction.js as follows.</p> <pre><code>var mongoose = require('mongoose'); mongoose.connect('mydb'); function Transaction(){ var actions = []; this.Add = function(query){ actions.push(query); }; this.Commit = function(){ for (var i = 0; i &lt; actions.length; i++) { actions[i](); } actions = []; }; } exports.Transaction = Transaction </code></pre> <p>I begin a new transaction with </p> <pre><code>var transactionlib = require('../DB/transaction'); var transaction = new transactionlib.Transaction(); </code></pre> <p>and call the transaction with</p> <pre><code>exports.PostHome = function(req, res){ var id = new ObjectId(req.body.objectId); transaction.Add(function(){ models.Story.findOne() .where('_id').equals(id) .exec(function(err, story){ saveOrUpdateStory(err, story, req, res); }); }); transaction.Commit(); }; </code></pre> <p>It should be mentioned that within saveOrUpdateStory there are more db calls taking place.</p> <p>This seems to work fine but now and then it appears to duplicate the command (a submission will be added twice). My current theory is that this has to do with two people submitting two commands at the same time and the transaction list being shared between both of them. I'm not sure of this though and would love some help. Any criticism on my code structure would also be appreciated as I'm still learning best practices in node.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload