Solving Nodejs async problems with Async

Source: Internet
Author: User


Project requirements: 1. The user's coal, water, electricity usage amount to the user weekly SMS reminder.

2. When the crawl of coal, water, electricity, the remaining amount of less than 10 yuan, the user to SMS reminders.

Database profiling: MongoDB built 4 tables divided into weekly reminders, water bills, electricity meters, gas meters

Weekly Reminder table: User name, User ID, whether on/off alert function, whether the user is associated with coal, water, electricity, user phone

Electricity meter: User name, user ID, community number, power supply company number, whether on/off alert, remaining electricity, user phone, update time, last update time, last update of remaining electricity,

Encounter problem Description: Use Nodejs development, to achieve every Saturday night SMS alert user function.

Through the weekly reminder table to get the user's information, according to the information to determine whether the user has Coal hydropower association, if associated to the corresponding coal and hydroelectric table query, and then the query information stitching into a new data structure back.

Because the Nodejs accesses the database, the asynchronous request gets incomplete data. So use the Async module.

Workaround:

Var  scheduleobj = require (' Node-schedule '); Var mongowake = require ('./ Mongo/wakesaturday '); Var mongoelectric = require ('./mongo/mongoelectric '); var mongoWater  = require ('./mongo/mongowater '); Var mongogas = require ('./mongo/mongogas ');// Refer to Async module Var async = require (' async '); Exports.schedule = function () {Var rule  = new scheduleobj.recurrencerule (); Var times = []; for (var i=1;  I<60; I=I+10) {Times.push (i);  }//weekly 6 rule.dayofweek =6;//on the server what time is used   is GMT   or China time Rule.hour =22;rule.second = 0;  var j = scheduleobj.schedulejob ( Rule, function () {                  //Remove the subject user information from the weekly reminder Mongowake.getall (function (err,result) {// console.log (result); if (err) {Return console.log (ERR);} Adding information to the mapseries requires the control process to follow the next query Async.mapseries (result,function (item,callbackmap) {item.result={};//  item.result[' electric '] = 10;// item.result[' water '] = 30;// item.result[' gas ']  = 60;// callbackmap (Null,item);//need to judge the information of the query, judge whether the user is related to coal and hydropower, And look for the results returned in each table query to join the final result in Async.waterfall ([Searchelectric, searchwater, searchgas], function () {    callbackmap (Null, item);}); Function searchelectric (done) {if (item.types.electric) {console.log (' electric  balance '); Mongoelectric.findbyid (Item.userid,function (err,eleresult) {// console.log (eleresult[0]);// obj.push ( Eleresult[0]); item.result[' Electric '] = eleresult[0];d one ();}); Else{done ();}}; Function searchwater (done) {if (item.types.water) {console.log (' water  balance '); Mongowater.findbyid (Item.userid,function (err,waterresult) {item.result[' water '] = waterresult[0];d one ();});} Else{done ();}}; function Searchgas (done) {if (Item.types.gas) {console.log (' gas  balance '); Mongogas.findbyid (Item.userid, function (err,gasresult) {item.result[' gas '] = gasresult[0];d one ();}); Else{done ();}};},function (err, results) {console.log (results); return results;});});



Solving Nodejs async problems with Async

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.