admin管理员组

文章数量:1333210

I heard that exec "returns a promise" so I'm using exec it do asynchronous calls. This question is inspired my other question. The mentator said my code is not working because :

You are using asynchronous code synchronously

I was trying to fix that by using the below code. Don't know if this code will make it not sync but I heard that promises help with that.

so I have this and I cannot create(save) the data but I can delete it. why cant I use the same pattern for create as i did for remove?

var Comp = require("./models/pany.js");
  var arr = [
    {name : "p1",industry : "industry1", ranking: 20},
    {name : "p2",industry : "industry2", ranking: 5},
    {name : "p3",industry : "industry3", ranking: 10}
  ]


Comp.find({}).exec()
    .then(function(docs){
        return Comp.remove({}).exec()
        .then(function(){
            console.log("deleted")
        })
    })
    .then(function(){
        return Comp.create(arr).exec()
        .then(function(data){
            console.log(data)
        })
    })

and can you help get to my original goal which was in my other question.

I heard that exec "returns a promise" so I'm using exec it do asynchronous calls. This question is inspired my other question. The mentator said my code is not working because :

You are using asynchronous code synchronously

I was trying to fix that by using the below code. Don't know if this code will make it not sync but I heard that promises help with that.

so I have this and I cannot create(save) the data but I can delete it. why cant I use the same pattern for create as i did for remove?

var Comp = require("./models/pany.js");
  var arr = [
    {name : "p1",industry : "industry1", ranking: 20},
    {name : "p2",industry : "industry2", ranking: 5},
    {name : "p3",industry : "industry3", ranking: 10}
  ]


Comp.find({}).exec()
    .then(function(docs){
        return Comp.remove({}).exec()
        .then(function(){
            console.log("deleted")
        })
    })
    .then(function(){
        return Comp.create(arr).exec()
        .then(function(data){
            console.log(data)
        })
    })

and can you help get to my original goal which was in my other question.

Share Improve this question edited May 23, 2017 at 12:17 CommunityBot 11 silver badge asked May 18, 2016 at 20:35 jack blankjack blank 5,1957 gold badges45 silver badges75 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

The then funtion does not return promise, the exec does!

So you need to do return Comp.remove({}).exec()

Comp.find({}).exec()
.then(function(docs){
    return Comp.remove({}).exec();
})
.then(function(result_of_remove){
    return Comp.create(arr).exec();
})
.then(function(result_of_create){
    ....
})

first of all you should confirm you mongoose version.

in older version:

Model.create(doc) returns a query object; call the exec method of the query will trigger the database operation and return a promise.

in new version (i am using 4.4.8) of mongoose Model.create(doc) and 'Model.remove(con)' returns a promise directly.

so check with your version to see if you need to remove some exec

last but not least add catch call to check if you got some errors, it helps when debug

Comp.find({}).exec()
.then(function(docs){
    return Comp.remove({}).exec();
})
.then(function(result_of_remove){
    return Comp.create(arr).exec();
})
.then(function(result_of_create){
    ....
})
.catch(function(error){
  console.log(error)
})

I normally use .exec() when I want to return a Promise when working with

Model.findOne(...).exec()

Model.create(...) returns a Promise.

The .exec() function does not exist for Model.create(...)

本文标签: javascriptreturn Modelcreate(arr)exec() is not working in mongooseStack Overflow