2017-07-30 4 views
0

ではありません:。model.removeは(...)modelname.removeを与える幹部が、私はこのエラーを取得しておく機能

Mongoose: invoice.remove({ _id: 3 }, {}) 
TypeError: invoiceRecord.remove(...).exec is not a function 

私は.execを削除しようとしたが、その後、それはERR(私の機能を認識しません、データ)

ここに私の機能は

var deleteInvoice =() => { 
    return new Promise((resolve, reject) => { 
    invoiceRecord.remove(
    ).exec(function (err, data) { 
     if (err) { 
      reject(new Error('deleteInvoice ERROR : ' + err)); 
      return; 
     } 
     if (data.result.n == 0) { 
      reject({code:"INVOICE_NOTFOUND", err:"This invoice could not be found: " + invoiceRecord._id}); 
      return; 
     } 
     resolve(data); 
    }) 
})}; 

invoiceRecordが既に削除するために必要なデータを持っているとき、私は、これをどのように行うことができますか?

これは動作します:

invoiceTable.remove (
    { _id: invoiceRecord._id } 
,function(err, data) { 

これにはない:正しい構文がhere文書化されている理由

答えて

1

invoiceRecord.remove (
     { _id: invoiceRecord._id } 
    ,function(err, data) { 

私は本当に理解していません。

すべての非同期マングースの方法は、すでに約束を返すので、あなたのコードは、これに書き換えることができます

var deleteInvoice =() => { 
    return invoiceRecord.remove().then(data => { 
    if (data.result.n === 0) { 
     // ATTN: it's better to throw Error instances 
     throw { code:"INVOICE_NOTFOUND", err:"This invoice could not be found: " + invoiceRecord._id }; 
    } 
    }, err => { 
    err.message = 'deleteInvoice ERROR : ' + err.message; 
    // Re-throw error 
    throw err; 
    }) 
})}; 
+0

おかげロバート - あなたがリンクして実際にマングースの例では、 'product.remove(機能(ERR、製品)を示しているためにあなたの 'invoiceRecord.remove()。then'の例は完璧に動作しました! – torbenrudgaard

+0

@torbenrudgaardなぜそれができないのか分かりません。私は、コールバックと約束の両方で、私はそれをしています。私は 'invoiceRecord'は通常のMongoose文書ですか? – robertklep

+0

はい、それはmongooseスキーマです。 – torbenrudgaard

関連する問題