mikeal / node.couchapp.js

Utility for writing couchapps.
Apache License 2.0
406 stars 78 forks source link

Fixed bug preventing multiple _attachment loads (in some cases) #36

Closed drobbins closed 12 years ago

drobbins commented 12 years ago

Fixes the following error:

[bug-demo] $ couchapp push app.js http://robbinsd:********@localhost:5984/bug-demo
The "sys" module is now called "util". It should have a similar interface.
Preparing.
Serializing.
PUT http://robbinsd:******@localhost:5984/bug-demo/_design/hey-look-a-bug
Serializing.
PUT http://robbinsd:******@localhost:5984/bug-demo/_design/hey-look-a-bug
Finished push. 1-16e1fb55a407fdcd1e840bb2eeecd6bb

/Users/robbinsd/Repos/node.couchapp.js/main.js:152
      if (resp.statusCode !== 201) throw new Error("Could not push document\n"
                                         ^
Error: Could not push document
{"error":"conflict","reason":"Document update conflict."}

    at Request._callback (/Users/robbinsd/Repos/node.couchapp.js/main.js:152:42)
    at Request.callback (/Users/robbinsd/Repos/node.couchapp.js/node_modules/request/main.js:104:22)
    at Request.<anonymous> (/Users/robbinsd/Repos/node.couchapp.js/node_modules/request/main.js:458:18)
    at Request.emit (events.js:67:17)
    at IncomingMessage.<anonymous> (/Users/robbinsd/Repos/node.couchapp.js/node_modules/request/main.js:423:16)
    at IncomingMessage.emit (events.js:88:20)
    at HTTPParser.onMessageComplete (http.js:137:23)
    at Socket.ondata (http.js:1125:24)
    at TCP.onread (net.js:334:27)

Which can happen when you have one or more attachment directories, and one of them takes a while to load (e.g. lots of files in sub-directories). E.g.:

var requirejs=require('requirejs');
requirejs.config({nodeRequire: require});

requirejs(['couchapp', 'path'], function(couchapp, path, fs){
    var ddoc = {
        _id: '_design/hey-look-a-bug',
        views : {},
        lists : {},
        shows : {},
    };

    couchapp.loadAttachments(ddoc, path.join(__dirname, "_attachments1"));
    couchapp.loadAttachments(ddoc, path.join(__dirname, "_attachments2"));

    module.exports = ddoc;
});

and

[bug-demo] $ ls -R
_attachments1   _attachments2   app.js      node_modules

./_attachments1:
att2.html   index.html  index1.html

./_attachments2:
4678829631_265b8468c2_o.jpg balh
4690519875_c75038c616_o.jpg fun
4690519875_c75038c616_o_2.jpg   index.html
4691152938_61493b9935_o.jpg index2.html

./_attachments2/balh:
4690506665_5d499b2f5c_o.jpg 4690519749_afa409a7d5_o.jpg
4690519749_afa409a7d5_o-1.jpg   blih
4690519749_afa409a7d5_o-1_2.jpg

./_attachments2/balh/blih:
4688402731_4357ccfbf0_o.jpg 4689036256_d903567d4e_o.jpg

./_attachments2/fun:
4679733012_2296d592e6_o.jpg 4688412657_64e1ca81b5_o_2.jpg
4688412657_64e1ca81b5_o.jpg 4689039444_8d3bde2c81_o.jpg

<snip>
max-mapper commented 12 years ago

nice! thanks