Note that there are some explanatory texts on larger screens.

plurals
  1. PONodeJS server callbacks
    primarykey
    data
    text
    <p>I'm writing a nodeJS REST api server with express for the forwarding part. Now part of what the server will do is backup different kinds of data, and there will be requests for backing up certain types of data I'll support and all of the requests that I support. To save on code duplication I was thinking of just calling all of the backup-individual-types-of-data functions from the request that deals with backing up everything.</p> <p>Individual type of data backup functions.</p> <pre><code>function verifyUserId(req, res, functionToGoTo) { req.mongoClient.collection('syncServiceAccounts', checkForAccount = function (err, collection) { collection.find({_id:req.objectID.createFromHexString(req.params.syncServiceUser)}).toArray(function (err, results) { if (results.length != 0) { functionToGoTo(req, res, results[0].share_key); } else { res.send(404); } }); }); }; //Set all server contacts as on device exports.syncServiceContactsSync = function (req, res) { console.log('syncServiceContactsSync'); verifyUserId(req, res, syncServiceContactsSync, sendContactsSyncResponse); //verifyUserId(req, res, syncServiceContactsSync); }; function syncServiceContactsSync(req, res, shareKey, callback) { //function syncServiceContactsSync(req, res, shareKey) { jsonObject = (req.body); var jsonResponse = new Array(); req.mongoClient.collection('syncService_Contacts', insertSyncServiceUser = function (err, collection) { collection.remove({share_key:shareKey}); for (var i = 0; i &lt; jsonObject.length; i++) { jsonObject[i].share_key = shareKey; collection.insert(jsonObject[i]); } collection.find({share_key:shareKey}).toArray(function (err, results) { for (var i = 0; i &lt; results.length; i++) { jsonResponse.push({contactID:results[i]._id}); } //res.json(jsonResponse, 200); callback(); }); }); }; function sendContactsSyncResponse() { res.json(jsonResponse, 200); } </code></pre> <p>And in the following function i'd like to be able to just call all the individual backup type. </p> <pre><code> //Set all server items as on the device exports.syncServiceSync = function (req, res) { console.log('syncServiceSync'); contactsObj = new Contacts(); contactsObj.syncService }; </code></pre> <p>I could just call the exported syncServiceContacsSync and it's equivalents for each type of data, but then i'd verify the userID for each new type of data, when I could do it once from the syncServiceSync. Is there a way to do that, and tell the syncServiceContactsSync what to do once it has inserted the data? Preferably it would go to a function together with the jsonResponse where I'd cascade it into the next data type.</p> <p>Also to note that the two blocks of files are in different files in the same folder.</p> <p>EDIT: To make it easier to understand. I have a system that backs up data type A, B, C, into their own mongo collections. I have another collection for the registered users for my app. In terms of requests, I have some that deal with backing up A, B, C individually and one that deals with all of them for a particular user account. In the one that deals with all of them, I thought it would be a good idea to use the functions that back up A, B, C so I don't have code duplication. As a result, what I'd kind want (but it might end up implemented differently) is that when I want to backup all of the data types, then I'd simply call the one that deals with A, then cascade into the one that deals with B, then the one with C. Besides simply cascading into each function, I'd also like to cascade some data (resource ID, so the IDs of the As, into the function that deals with Bs, then the IDs of A and B into the one that deals with C, and then into a final function that returns all of these IDs). The problem that I have is that the way the code is written now, every time I'd cascade, I'd recheck to see if I can find the user in the user collection, and that's just kinda stupid.</p>
    singulars
    1. This table or related slice is empty.
    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