NodeJS MongoDB异步调用序列正在受到干扰

时间:2018-01-02 09:10:56

标签: node.js mongodb

我正在使用MongoDB的外键策略来映射订阅表中的sub_id ...而映射我已经给出了调试目的的控制台日志,因为我从3天后没有得到适当的结果...我希望它迭代每个结果光标由first find()生成。

这里是NodeJS服务器API

connection((db) => {
    db.collection('gyms').find().toArray(function(err, main_results) {
        if (err) throw err;

        main_results.forEach(main_result => {
            subscriptions = main_result.subscriptions;
            console.log(subscriptions);
            console.log(subscriptions.length);
            if (subscriptions.length == 1) {
                latest_sub = subscriptions[0];
            } else {
                latest_sub = subscriptions[subscriptions.length - 1];
            }
            console.log(latest_sub);
            db.collection('subscriptions')
                .findOne({
                    _id: ObjectId(latest_sub.sub_id)
                }, function(err, sub_results) {
                    if (err) throw err;
                    console.log(main_result);
                    console.log(latest_sub)
                    console.log(sub_results);
                    main_result.latest_plan = sub_results.name;
                    var date = new Date(latest_sub.date);
                    console.log("kab kiya? " + date);
                    var newDate = new Date(date.setTime(date.getTime() + sub_results.validity * 86400000));
                    console.log("kab khatm hoga?" + newDate);
                    validity = Math.floor((newDate - new Date()) / (1000 * 3600 * 24));
                    console.log("validity" + validity);
                    console.log("--------------------------------------");
                    if (validity >= 0) {
                        main_result.validity = validity;
                    } else {
                        main_result.validity = -1;
                    }
                });
        });

        function myRes() {
            res.json({
                success: true,
                data: main_results
            })
        }
        setTimeout(myRes, 3000);
    });
});

控制台记录

[ { sub_id: '5a4b365f378e1c0b18862582',
    date: 2017-12-19T18:30:00.000Z } ]
1
{ sub_id: '5a4b365f378e1c0b18862582',
  date: 2017-12-19T18:30:00.000Z }
[ { sub_id: '5a4b3666378e1c0b18862583',
    date: 2017-12-19T18:30:00.000Z } ]
1
{ sub_id: '5a4b3666378e1c0b18862583',
  date: 2017-12-19T18:30:00.000Z }
[ { sub_id: '5a4b366f378e1c0b18862584',
    date: 2017-12-19T18:30:00.000Z } ]
1
{ sub_id: '5a4b366f378e1c0b18862584',
  date: 2017-12-19T18:30:00.000Z }
[ { sub_id: '5a4b3666378e1c0b18862583',
    date: 2017-12-19T18:30:00.000Z } ]
1
{ sub_id: '5a4b3666378e1c0b18862583',
  date: 2017-12-19T18:30:00.000Z }
[ { sub_id: '5a4b366f378e1c0b18862584',
    date: 2017-12-28T18:30:00.000Z } ]
1
{ sub_id: '5a4b366f378e1c0b18862584',
  date: 2017-12-28T18:30:00.000Z }
{ _id: 5a4b368c378e1c0b18862585,
  join_date: '2017/12/20',
  gender: 'Male',
  owner: 'A',
  name: 'A',
  address: '14 shree om darshan, sai nagar',
  state: '—',
  city: 'Nashik',
  pin_code: 422004,
  phone: 9370555444,
  email: 'alpesh.vasani857@gmail.com',
  username: 'A',
  subscriptions:
   [ { sub_id: '5a4b365f378e1c0b18862582',
       date: 2017-12-19T18:30:00.000Z } ] }
{ sub_id: '5a4b366f378e1c0b18862584',
  date: 2017-12-28T18:30:00.000Z }
{ _id: 5a4b365f378e1c0b18862582,
  name: 'for A',
  validity: 111,
  price: 111,
  availability: true }
kab kiya? Fri Dec 29 2017 00:00:00 GMT+0530 (India Standard Time)
kab khatm hoga?Thu Apr 19 2018 00:00:00 GMT+0530 (India Standard Time)
validity106
--------------------------------------
{ _id: 5a4b3697378e1c0b18862586,
  join_date: '2017/12/20',
  gender: 'Male',
  owner: 'B',
  name: 'B',
  address: '14 shree om darshan, sai nagar',
  state: '—',
  city: 'Nashik',
  pin_code: 422004,
  phone: 9370555444,
  email: 'alpesh.vasani857@gmail.com',
  username: 'B',
  subscriptions:
   [ { sub_id: '5a4b3666378e1c0b18862583',
       date: 2017-12-19T18:30:00.000Z } ] }
{ sub_id: '5a4b366f378e1c0b18862584',
  date: 2017-12-28T18:30:00.000Z }
{ _id: 5a4b3666378e1c0b18862583,
  name: 'for B',
  validity: 222,
  price: 222,
  availability: true }
kab kiya? Fri Dec 29 2017 00:00:00 GMT+0530 (India Standard Time)
kab khatm hoga?Wed Aug 08 2018 00:00:00 GMT+0530 (India Standard Time)
validity217
--------------------------------------
{ _id: 5a4b36a3378e1c0b18862587,
  join_date: '2017/12/20',
  gender: 'Male',
  owner: 'C',
  name: 'C',
  address: '14 shree om darshan, sai nagar',
  state: '—',
  city: 'Nashik',
  pin_code: 422004,
  phone: 9370555444,
  email: 'alpesh.vasani857@gmail.com',
  username: 'C',
  subscriptions:
   [ { sub_id: '5a4b366f378e1c0b18862584',
       date: 2017-12-19T18:30:00.000Z } ] }
{ sub_id: '5a4b366f378e1c0b18862584',
  date: 2017-12-28T18:30:00.000Z }
{ _id: 5a4b366f378e1c0b18862584,
  name: 'for C',
  validity: 333,
  price: 333,
  availability: true }
kab kiya? Fri Dec 29 2017 00:00:00 GMT+0530 (India Standard Time)
kab khatm hoga?Tue Nov 27 2018 00:00:00 GMT+0530 (India Standard Time)
validity328
--------------------------------------
{ _id: 5a4b36c8378e1c0b18862588,
  join_date: '2017/12/20',
  gender: 'Male',
  owner: 'C',
  name: 'C',
  address: '14 shree om darshan, sai nagar',
  state: '—',
  city: 'Nashik',
  pin_code: 422004,
  phone: 9370555444,
  email: 'alpesh.vasani857@gmail.com',
  username: 'C',
  subscriptions:
   [ { sub_id: '5a4b3666378e1c0b18862583',
       date: 2017-12-19T18:30:00.000Z } ] }
{ sub_id: '5a4b366f378e1c0b18862584',
  date: 2017-12-28T18:30:00.000Z }
{ _id: 5a4b3666378e1c0b18862583,
  name: 'for B',
  validity: 222,
  price: 222,
  availability: true }
kab kiya? Fri Dec 29 2017 00:00:00 GMT+0530 (India Standard Time)
kab khatm hoga?Wed Aug 08 2018 00:00:00 GMT+0530 (India Standard Time)
validity217
--------------------------------------
{ _id: 5a4b36dc378e1c0b18862589,
  join_date: '2017/12/29',
  gender: 'Male',
  owner: 'C',
  name: 'C',
  address: '14 shree om darshan, sai nagar',
  state: '—',
  city: 'Nashik',
  pin_code: 422004,
  phone: 9370555444,
  email: 'alpesh.vasani857@gmail.com',
  username: 'C',
  subscriptions:
   [ { sub_id: '5a4b366f378e1c0b18862584',
       date: 2017-12-28T18:30:00.000Z } ] }
{ sub_id: '5a4b366f378e1c0b18862584',
  date: 2017-12-28T18:30:00.000Z }
{ _id: 5a4b366f378e1c0b18862584,
  name: 'for C',
  validity: 333,
  price: 333,
  availability: true }
kab kiya? Fri Dec 29 2017 00:00:00 GMT+0530 (India Standard Time)
kab khatm hoga?Tue Nov 27 2018 00:00:00 GMT+0530 (India Standard Time)
validity328
--------------------------------------

0 个答案:

没有答案