And please remember that couchdb works with "eventual consistency", ot does not allways guaranty that read after write os allways in sync
Angel Java Lopez <[email protected]> escreveu: >Umm... I don't use CouchDB, nano module. But it looks as a callback >problem. You are running CouchRead() inmmediately AFTER CouchWrite(), >but >this function has still pending to call its own callbacks. > >So, a quick alternative approach (not tested): > >var couchdb = require('nano')('http://localhost:5984/'); >var db = couchdb.use('udoodb'); >CouchWrite(); >// CouchRead(); <-- not here > > >function CouchWrite() { >couchdb.db.destroy('udoodb', function() >{ >couchdb.db.create('udoodb', function(err) { >if (err) {throw err; } >var doc1 = { sw1:false, sw2:false, sw3:false }; >db.insert(doc1,'4662745328718336f506f9d2d3010c0c', function(err) { >if (err) {throw err; } > >// Call CouchRead in the callback of db.insert >CouchRead(); > >}); >}); >}); >} > >function CouchRead() { >db.get('4662745328718336f506f9d2d3010c0c', function(err, val) { >console.log('Switches =', val); >}); >} > > >A more flexible approach: > >var couchdb = require('nano')('http://localhost:5984/'); >var db = couchdb.use('udoodb'); >CouchWrite(CouchRead); // pass CouchRead as the callback of CouchWrite >// CouchRead(); <-- not here > > >function CouchWrite(cb) { >couchdb.db.destroy('udoodb', function() >{ >couchdb.db.create('udoodb', function(err) { >if (err) {throw err; } >var doc1 = { sw1:false, sw2:false, sw3:false }; >db.insert(doc1,'4662745328718336f506f9d2d3010c0c', function(err) { >if (err) {throw err; } > >// Call the callback cb >cb(); > >}); >}); >}); >} > > >But the preferred Node way is to have callbacks a la function(err, >result) > > >var couchdb = require('nano')('http://localhost:5984/'); >var db = couchdb.use('udoodb'); >CouchWrite(CouchRead); // pass CouchRead as the callback of CouchWrite >// CouchRead(); <-- not here > > >function CouchWrite(cb) { >couchdb.db.destroy('udoodb', function() >{ >couchdb.db.create('udoodb', function(err) { >if (err) {throw err; } >var doc1 = { sw1:false, sw2:false, sw3:false }; >db.insert(doc1,'4662745328718336f506f9d2d3010c0c', cb); >}); >}); >} > >function CouchRead(err, data) { >if (err) throw err; > >db.get('4662745328718336f506f9d2d3010c0c', function(err, val) { >console.log('Switches =', val); >}); >} > >Angel "Java" Lopez >@ajlopez > > > >On Mon, Feb 17, 2014 at 7:00 AM, Reiko Nötzold < >[email protected]> wrote: > >> Hi, >> >> i developed in node.js and CouchDB and have a problem with "function" >and >> local/global variables. >> My Code write and read in CouchDB, is running. But when both >CouchWrite(); >> and CouchRead(); "Switches = undefined" >> CouchWrite(); "or" CouchRead(); is running. >> >> >> var couchdb = require('nano')('http://localhost:5984/'); >> var db = couchdb.use('udoodb'); >> CouchWrite(); >> CouchRead(); >> >> >> function CouchWrite() { >> couchdb.db.destroy('udoodb', function() >> { >> couchdb.db.create('udoodb', function(err) { >> if (err) {throw err; } >> var doc1 = { sw1:false, sw2:false, sw3:false }; >> db.insert(doc1,'4662745328718336f506f9d2d3010c0c', function(err) { >> if (err) {throw err; } >> }); >> }); >> }); >> } >> >> function CouchRead() { >> db.get('4662745328718336f506f9d2d3010c0c', function(err, val) { >> console.log('Switches =', val); >> }); >> } >> >> Thanks >> Reiko >> >> -- >> -- >> Job Board: http://jobs.nodejs.org/ >> Posting guidelines: >> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >> You received this message because you are subscribed to the Google >> Groups "nodejs" group. >> To post to this group, send email to [email protected] >> To unsubscribe from this group, send email to >> [email protected] >> For more options, visit this group at >> http://groups.google.com/group/nodejs?hl=en?hl=en >> >> --- >> You received this message because you are subscribed to the Google >Groups >> "nodejs" group. >> To unsubscribe from this group and stop receiving emails from it, >send an >> email to [email protected]. >> For more options, visit https://groups.google.com/groups/opt_out. >> > >-- >-- >Job Board: http://jobs.nodejs.org/ >Posting guidelines: >https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >You received this message because you are subscribed to the Google >Groups "nodejs" group. >To post to this group, send email to [email protected] >To unsubscribe from this group, send email to >[email protected] >For more options, visit this group at >http://groups.google.com/group/nodejs?hl=en?hl=en > >--- >You received this message because you are subscribed to the Google >Groups "nodejs" group. >To unsubscribe from this group and stop receiving emails from it, send >an email to [email protected]. >For more options, visit https://groups.google.com/groups/opt_out. -- E-mail enviado do meu celular usando Kaiten Mail. Por favor, desculpe minha brevidade. -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
