request
とcheerio
を使用して、ポストパラメータからのリンクから各動画のsrcを取得しようとしています。各ループごとにリクエストURLを変更するにはどうすればよいですか?
各動画のsrcを取得するには、すべてのループでrequestオプションのurlを変更する必要があります。しかし、以下のsrcのようなコードを取得しようとすると、リクエストのurlを変更するのがリクエストよりも速いため、リクエストのURLを変更することはリクエストが完了する前に行われます。私が欲しいものを実現するために私は何ができますか?
ここでコード
let opt = {
transform: function(body) {
return cheerio.load(body);
}
};
router.post('/api', function(req, res) {
let idArray = req.body.data;
for(var i=0; i<idArray.length; i++) {
opt.uri = baseURL + idArray[i];
request(opt)
.then(function($) {
console.log($('video').src);
}
ログ(それは同じことを印刷します) https://example.mp4/asdfasdf https://example.mp4/asdfasdf
私はこのコードを使用する場合、私はidArrayから未定義の取得EDIT
[i]を
ですfor(var i=0; i<idArray.length; i++) {
console.log("Before", baseURL + idArray[i])
rpap(baseURL + idArray[i])
.then(function($) {
console.log(idArray);
console.log("After", baseURL + idArray[i]);
}
ログ
before http://example.com/adsfasdf
before http://example.com/famvvasd
after http://example.com/undefined
after http://example.com/undefined
「... opt、uri」の意味を理解できません。 –
@PhillipYSこれは[ES6のデストラクション割り当て](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)の一部です。基本的に '... opt'は' opt'の中の全てのプロパティを書き込まれたオブジェクトに展開します。 '{uri}'は有効なES6であり、 '{uri:uri} 'と同じ意味です。ですから、処理されたコードは 'request({transform:function(){...}、uri:uri})'と似ています。 –