Mongoose: Model.create Promiseは解決されましたが、ドキュメントは保存されていません

作成日 2018年02月01日  ·  3コメント  ·  ソース: Automattic/mongoose

こんにちは、みんな、

私はマングースバージョン5.0.3とノードバージョン9.4.0を使用しています。 マングースのドキュメントによると、model.createはpromiseを返す必要があります。これは、作成されたドキュメントがDBに保存されるときに解決されると思います。 ただし、以下のコードに示されているように、ドキュメントがDBに保存される前にpromiseが解決されているようです。

async function test(){

    let schema = new mongoose.Schema({a: String});

    let model = mongoose.model('test', schema);

    await model.remove({}).exec();

    await model.create({ a: 'test'}, 
       (err,result)=> {console.log('created');});

    await model.findOneAndUpdate(
        { a : 'test' } , 
        { a: 'newValue'}
    )
    .exec((err, result) => {
        console.log('update : '+result);
    });

    await model.find({a: 'test'},(err,result) => {
        console.log(result); 
    });

}

output in terminal : 
    update : null
    found :
    created

findOneAndUpdateはドキュメントを見つけられません。 それに加えて、「created」がターミナルの最後に表示されるため、createメソッドのコールバックは、awaitが非同期タスク「create」の実行を待機していないかのように実行されます。

ただし、以下に示すように、createのコールバックがトリガーされると解決されるpromiseを追加することで、期待される結果が得られます。

async function test(){

    let schema = new mongoose.Schema({a: String});

    let model = mongoose.model('test', schema);

    await model.remove({}).exec();

    await new Promise((resolve,reject) => {
        model.create({ a: 'test'}, 
       (err,result)=> {
            console.log('created');
            resolve();
        });
    });

    await model.findOneAndUpdate(
        { a : 'test' } , 
        { a: 'newValue'}
    )
    .exec((err, result) => {
        console.log('update : '+result);
    });

    await model.find({a: 'newValue'},(err,result) => {
        console.log('found : ' +result); 
    });

}

output in terminal: 
     created
     update : { _id: 5a735fbc1fe826233014d62d, a: 'test', __v: 0 }
     found : { _id: 5a735fbc1fe826233014d62d, a: 'newValue', __v: 0 }

これで、期待どおりの結果が得られました。

DBを操作するすべての関数は、解決された場合に操作が完了したことを示すという約束を返します。具体的にはクエリです。 厳密に言えばクエリオブジェクトを返さないのですが、model.createの場合もそうだったと思います。 また、ドキュメントがDBで作成されたことを示していないため、返送された約束が何を意味するのか疑問に思います。 おそらく私は全体のポイントを逃したが、それは少し曖昧だと思う

最も参考になるコメント

4.xのドキュメントを読んだことを覚えていると思いますが、model.createにコールバックを渡しても、promiseは返されません。

let x = model.create({ a: 'test' }, () => {})
  x.then(console.log(x)) //TypeError: Cannot read property 'then' of undefined

コールバックを引き出すと:

let x = model.create({ a: 'test' })
  x.then(console.log(x)) // Promise { <pending> }

model.jsの5.xソースは、utils.promiseOrCallbackの呼び出しでこれを実行します

全てのコメント3件

4.xのドキュメントを読んだことを覚えていると思いますが、model.createにコールバックを渡しても、promiseは返されません。

let x = model.create({ a: 'test' }, () => {})
  x.then(console.log(x)) //TypeError: Cannot read property 'then' of undefined

コールバックを引き出すと:

let x = model.create({ a: 'test' })
  x.then(console.log(x)) // Promise { <pending> }

model.jsの5.xソースは、utils.promiseOrCallbackの呼び出しでこれを実行します

はい@CodeurSauvageで終了できますか?

@CodeurSauvage @lineusは正しいです。コールバックが指定されている場合、 。 次のものを置き換えると、スクリプトが機能します。

    await model.create({ a: 'test'}, 
       (err,result)=> {console.log('created');});

と:

    await model.create({ a: 'test'}).then(result => console.log('created'));
このページは役に立ちましたか?
0 / 5 - 0 評価