How JavaScript solves Mongoose data query asynchronous operations tutorial

Source: Internet
Author: User
Tags emit event listener findone

When I am operating in the node environment mongoose, found that I through the nested form of query to the data loop into an array, the end of the array has been empty, after Baidu, still no fruit, finally asked the group of the Great God, the original is mongoose and node asynchronous caused , Baidu to solve the solution, the following is my use of a method I think is relatively simple.

The original code
Varshoppingmodel = Global.dbHandle.getModel (' shopping ');
Varcartsshop = [];
for (Vari = 0;i<carts.length;i++) {
Shoppingmodel.findone ({title:carts[i].title},function (err,shops) {
if (err) {
Returnnext (ERR);
}else{
Cartsshop.push (shops);
}
});
}
Console.log (Cartsshop);//[]

Through the introduction of node itself contains methods events.

Events

Node.js all asynchronous I/O operations will send an event to the event queue when they are completed.

Many objects in Node.js distribute events: A Net.server object distributes an event each time a new connection is made, and a Fs.readstream object emits an event when the file is opened. All of the objects that generate the event are events. An instance of Eventemitter.

Eventemitter class

The events module provides only one object: events. Eventemitter. The core of Eventemitter is the encapsulation of event triggering and event listener functions.
You can access the module by require ("events").

Here's an example,

Event.js file
Vareventemitter = require (' Events '). Eventemitter;
Varevent =neweventemitter ();
Event.on (' Some_event ', function () {
Console.log (' some_event event Trigger ');
});
settimeout (function () {
Event.emit (' some_event ');
}, 1000);

Make improvements to my own code, as follows:

Varshoppingmodel = Global.dbHandle.getModel (' shopping ');
Varcartsshop = [];
Varobj;
Varj = 0;
Varmyeventemitter =newevents. Eventemitter ();
Myeventemitter.on (' Next ', Addresult);
Functionaddresult () {
Cartsshop.push (obj);
j + +;
if (j==carts.length) {
Console.log (Cartsshop);
Res.json (Cartsshop);
}
}
for (Vari = 0;i<carts.length;i++) {
Varii = i;
Shoppingmodel.findone ({title:carts[ii].title},function (err,shops) {
if (err) {
Returnnext (ERR);
}else{
obj = shops;
Myeventemitter.emit (' Next ');
}
});
}

Of course, use must be introduced before

Varevents = require (' events ');

Finally, the data is successfully removed and returned to the client.

Related Article

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.