使用mongoose承诺的麻烦

有人能告诉我为什么这个承诺设置不起作用。 它应该删除文档,然后添加它然后find它然后控制它。 这不是安慰数据。

var Comp = require("./models/company.js"); var arr = [ {name : "comp1",industry : "industry1", ranking: 20}, {name : "comp2",industry : "industry2", ranking: 5}, {name : "comp3",industry : "industry3", ranking: 10} ] var output = {}; var promise = Comp.find({}).exec() promise.then(function(res){ console.log("removed") return Comp.remove({}).exec() }) .then(function(){ return Comp.create(arr).exec() }) .then(function(data){ return Comp.find({}).exec(); }) .then(function(data){ console.log(data) }) 

编辑::

发现了一个错误

[TypeError: Comp.create(...).exec is not a function]

Model.create返回一个承诺,所以不需要在返回值上调用.exec()

 .then(function(){ return Comp.create(arr); })