About js blocking and non-blocking, synchronous and asynchronous, problems encountered?

  node.js, question

When I save the category list, I also need to save multiple categories at the same time, and then take out the _id value of each category.
Let’s start with the code.

//This is for preservation
 var newCatId=[];
 if(movieObj.catetoryName){
 var catetories = movieObj.catetoryName.split('|');  //divide multiple categories
 for(var i=0,len=catetories.length;  i<len;  I plus)
 var el = catetories[i];
 CatetoryModel.saveModel(req,res,next,el);  //and then circularly store
 bracket
 for(var i=0,len=catetories.length;  i<len;  I plus)
 var el = catetories[i];
 Find ({name: el}, function (err, cat) {//then recycle and take out.
 if(err){console.log(err)}
 console.log('cat---------------')
 Log (cat)//print out the removed cat.
 console.log('cat---------------')
 NewCatId.push(cat._id)// store _idpush into array
 })
 bracket
 
 console.log('newCatId--------------')
 Log (newcatid)//print the _id array
 console.log('newCatId--------------')
 
 bracket
 res.end()

Here’s how I saved it.

//Save
 exports.saveModel = function (req,res,next,cat) {
 var _cat = new Catetory({
 name:cat
 })
 _cat.save(function (err,cat) {
 if(err){
 console.log(err)
 bracket
 Log ('save-')//console to print, save successfully
 next();
 })
 bracket

My first expected printing sequence should be

//  console.log('save-------------') --->
 // console.log(cat)  --->
 // console.log(newCatId)  --->

But what actually came out was
图片描述
图片描述
This surprised me … then I thought … come out first. // console.log(newCatId) this is normal … this is what I can think of … but according to the above figure … then he should not put the//console.log (‘save—‘) this guy in the last place. I added a promise to try the code as follows

if(movieObj.catetoryName){
 var catetories = movieObj.catetoryName.split('|');
 new Promise(function () {
 catetories.map(function (el) {
 CatetoryModel.saveModel(req,res,next,el)
 })
 }).then(function(){
 catetories.map(function (el) {
 // console.log(el)
 Catetory.find({name:el},function (err,cat) {
 if(err){console.log(err)}
 console.log('cat---------------')
 console.log(cat)
 console.log('cat---------------')
 newCatId.push(cat._id)
 })
 console.log('newCatId--------------')
 console.log(newCatId)
 console.log('newCatId--------------')
 res.end()
 })
 })
 bracket

The result is not what I want … either!图片描述
And the page shows Cannot POST /admin/movie/new

This is why …

My demand is this图片描述
When the movie is entered, it is classified. If there is, there are moral choices below. If there is no, fill it in yourself. Then take out the newly saved category, take out the new category _id, and store the newly added movie _id value in each category separately. So I have to go round and round again. Is there any other way besides this design?
The Great God of Kneeling xie instructed me! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! one

I didn’t look at it very carefully, but I don’t think you understand asynchrony very well. Moreover, mongodb should not be used like this. It is easy to make mistakes when making asynchronous requests in a loop. There are too many uncertainties. It is recommended that you use the method of batch operation of data, prepare the data first, and then save the data to the database inside at one time. Since mongodb is used, then don’t break up the data too loosely. It is ok for a data to contain all the information.