不能在猫鼬中使用诺言 [英] trouble using promises with mongoose

查看:73
本文介绍了不能在猫鼬中使用诺言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么这个承诺设置不起作用.它应该删除文档,然后添加它,然后找到它,然后进行控制台.这不是在安慰数据.

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

:

遇到错误

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

推荐答案

Model.create 返回一个Promise,因此无需在返回值上调用.exec().

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

这篇关于不能在猫鼬中使用诺言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆