1

よくわからないエラーが出ます。関数の配列で async.waterfall を呼び出しています。関数は、わかりやすくするために「短縮」されています。

FabricCommand.prototype.do = function (callback, undoArray) {
    var self = this;

    if (undoArray === undefined) {
        undoArray = [];
    }

    undoArray.push(self);
    callback(null, undoArray);
};

以下に示すように配列を作成します。doCommands は配列であり、オブジェクトはそのように追加されます。

doCommands.push(fabricCommand.do.bind(fabricCommand));

ウォーターフォールのセットアップ:

async.waterfall(
    doCommands,
    function(err, undoCommands){
        if (err) {
           // do something ...
        }
        else {
            console.log('we succeeded with all the do commands... and there are '
                + undoCommands.length
                + ' in the undoCommands but we will disregard it...');
        }
    }
);

このコードを実行すると、最初に FabricCommand.do 関数を使用して、undoCommands 配列を割り当て、それに 1 つ追加します。次回は、配列要素を追加しようとすると、次のエラーが表示されます。

undoArray.push(something);
          ^ TypeError: Object function (err) {
            if (err) {
                callback.apply(null, arguments);
                callback = function () {};
            }
            else {
                var args = Array.prototype.slice.call(arguments, 1);
                var next = iterator.next();
                if (next) {
                    args.push(wrapIterator(next));
                }
                else {
                    args.push(callback);
                }
                async.setImmediate(function () {
                    iterator.apply(null, args);
                });
            }
        } has no method 'push'

誰かが私が間違っていることを見ることができますか?

4

1 に答える 1

2

によって実行される関数にはasync.waterfall、次の署名が必要です。

function(arg, callback) { … }

または、複数の引数を指定して:

function(arg1, arg2, callback) { … }

あなたの場合、2 つのパラメーターを単純に逆にしました。

 FabricCommand.prototype.do = function (callback, undoArray) { … }

callbackに格納するための値undoArrayを受け取り、 、つまり関数undoArray用の値を受け取りました。これが、この奇妙なエラー ( ) に遭遇した理由です。callbackfunction […] has no method 'push'

パラメータを正しい順序で配置する必要があります。

 FabricCommand.prototype.do = function (undoArray, callback) { … }

2 つ目の問題は、ウォーターフォールの最初の関数が 1 つのパラメーター (コールバック) のみを受け取ることです (これはウォーターフォールの最初の関数であるため、受け取る値がないためです)。解決策は、引数の数を確認することです。

if (Array.prototype.slice.apply(arguments).length === 1) {
    callback = undoArray;
    undoArray = undefined;
}

これが作業の要点です。

于 2013-12-23T19:06:07.633 に答える