2017-09-07 1 views
0

だから今の出力は、次のとおりです。スーパー・リクエストが他のリクエストに依存している場合、どのようにチェーン化できますか?

Warning: superagent request was sent twice, because both .end() and .then() were called. Never call .end() if you use promises 
Warning: .end() was called twice. This is not supported in superagent 
GET /api/things 200 3.009 ms - 2414 
superagent: double callback bug 
WARNING 

事があり、私はバックのETag機能をテストするために、.end()を呼び出してから取得resオブジェクトが必要です。それは第2の.end()に入ることはなく、そのコンソールログは表示されません。

最も内側の.catch(done)が呼び出されています。

it('should return things', done => { 
    const promises = []; 

    promises.push(Thing.create(testThing)); 
    promises.push(Thing.create(testThing)); 

    Promise.all(promises) 
    .then(things => { 
     request(app) 
     .get('/api/things') 
     .expect(200) 
     .end((err, res) => { 
      const etag = res.get('ETag') 
      if (err) 
      return done(err); 
      request(app) 
      .get('/api/things') 
      .set('If-None-Match', etag) 
      .expect(304) 
      .end((err, res) => { 
       console.log('are we able to make it back here?'); 
       expect(err).to.not.exist; 
       expect(res.body.data).to.be.undefined; 
       expect(res.get('ETag')).to.be.equal(etag); 
       return done(err); 
      }) 
     }) 
     .catch(done); 
    }) 
    .catch(done); 
}); 
}); 

なぜこのようなことが起こっているのか、どのようにそのようなことをテストするのかについてのアイデアはありますか?

答えて

0

は私がやっていた、それを考え出した次

if (req.header('If-None-Match') === allThingsEtag) { 
    // dont send allThings 
    res.status(304); 
    res.json(); 
} 

Iを:それはそうのように、それを返すために知っていたので、私は実際に応答の本文を設定するために必要な

if (req.header('If-None-Match') === allThingsEtag) { 
    // dont send allThings 
    res.status(304); 
} 

私の場合はこれが信じられないほど具体的だと知っていますが、誰もがこの問題にぶつかる機会がありません。それは私が思いついた解決策です。だから多分あなたのAPIコードをチェックしてください。

.end()を回避するために、私は.then()を使用するようにテストを変更しました。

+0

私はすでにこれを解決していますが、明示的にjsonレスポンスとコンテンツタイプが必要ない場合は、 'sendStatus'関数を使用することもできます。 'res.sendStatus(304);' – Mikelax

関連する問題