1

Node.jsスクレイパーによって構築されているかなり単純な javascript/jquery オブジェクトがあります。すべてが正常に機能します (obj.prod_name_select対応するものは jquery セレクターを返し、ノードで問題なく解析しています)。

オブジェクトは次のとおりです。

var product = {
    name        : $(obj.prod_name_select).text(),
    systemName  : function(){
                    return product.name.replace(/\s+/g, ' ');
                    },
    gender      : obj.gender,
    infoLink    : link,
    designer    : $(obj.label_name_select).first().text(),
    description : function(){
                    var text = $(obj.description).each(function() {
                                var content = $(this).contents();
                                $(this).replaceWith(content);
                            });
                    return text;
                    },
    price       : $(obj.price_select).first().text(),
    store       : obj.store,
    category    : obj.general_cat,
    spec_cat    : obj.spec_cat
}
console.log(product);

これをノードで実行すると、関数によって設定されるプロパティを除いて、すべて正常に動作します。これら[FUNCTION]はノードに戻ります。コンソールからのログは次のとおりです。

{ name: 'BAGGY PANTS!T',
  systemName: [Function],
  gender: 'men',
  infoLink: 'http://www.hereisthecorrecturl.com',
  designer: 'Fancy Designer',
  description: [Function],
  price: '$180.00',
  store: 'Correct Store Name',
  category: 'Correct Category',
  spec_cat: 'Correct Category' }

これは非同期の問題ですか? プロパティ関数が実行される前に console.log が発生しているようです。これを解決する最善の方法は何ですか?

4

1 に答える 1

1

関数を割り当てるだけでなく、すぐに実行する必要があります。

description : (function(){
    return $(obj.description).each(function() {
        var content = $(this).contents();
        $(this).replaceWith(content);
    });
}()),
于 2013-06-05T01:32:40.780 に答える