undefinedfix
Sign in

How to cycle query mongodb database in nodejs?

pbluu edited in Fri, 23 Sep 2022
var mongoose = require('mongoose'); 
require('../../models/new'); 
var New= mongoose.model('New');
var async = require('async');

const data0 = ['北京', '天津', '上海', '重庆', '河北', '河南', '云南']
exports.mapdata = function (req, res) {
  try {
    // start(req, res)
    run(req, res)
  } catch (e) {
    res.status(402).json({ "function error": e.message })
  }
}
function run(req, res) {
  let mapdata = {}
  const list = []
  async.mapSeries(data0, function (item, callback) {
    // console.log('1.3 enter: ' + item);
    setTimeout(function () {
      // console.log('1.3 handle: ' + item);
      New.find({ 'properties.地区': item, 'properties.类型': '娱乐新闻' }).count()
        .then(function (values1) {
          New.find({ 'properties.地区': item, 'properties.类型': '实时新闻' }).count()
            .then(function (values2) {
              mapdata ['name'] = item
              mapdata ['value1'] = values1
              mapdata ['value2'] = values2
              mapdata ['value'] = values1 + values2
              list.push(mapdata )
              console.log(list)
              callback(null, list);
            })
            console.log(list)
        })
        console.log(list)
    }, 0);
  }, function (err, results) {
    // console.log('1.3 err: ', err);
    // console.log('1.3 results: ', results);
  });
}

The above is the code in the file, using nodejs + express , The database uses mongodb

Now you need to loop the query and return it to the front end, but the result is basically the last region in the array

As follows:

Novice white a, ask for advice

0 Replies