jack blank
jack blank

Reputation: 5203

trouble using promises with mongoose

Can someone tell me why this promise setup doesn't work. It supposed to remove the docs then add it then find it then console it. It's not consoling the data.

  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)
    })

EDIT : :

caught an error

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

Upvotes: 2

Views: 356

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

Model.create returns a promise, so there's no need to call .exec() on the return value.

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

Upvotes: 3

Related Questions