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 badges3 Answers
Reset to default 4The 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
版权声明:本文标题:javascript - return Model.create(arr).exec() is not working in mongoose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742285443a2446845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论