2016-07-07 6 views
0

Ich bin jetzt mit meinem Projekt mit NodeJS und Busboy fest. Ich versuche, alle Variablen von meinem Router abzurufen und an die Controller zu senden.Knoten JS - Busboy Parameter abrufen

app.js (Routen)

router.post('/product', function (req, res) { 
var fields = {}; 
req.busboy.on('field', function(fieldname, val) { 
     fields[fieldname] = val; 
}); 
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) { 
    fields[filename] = file; 
}); 
req.pipe(req.busboy); 


productController.addNewProduct(fields, function (data) { 
    res.json(data); 
}); 
}); 

ProductController.js

addNewProduct: function (params, callback) { 
    productLogic.addNewProduct(params, function (data) { 
     callback(data); 
    }); 
}, 

ProductLogic.js

addNewProduct: function (params, callback) { 
    Product.findOne({ name: params.name }, function (err, product) { 
     if(err) callback({ status: false, message: err }); 

     if(product) 
      callback({ status: false, message: 'Produk sudah ada.' }); 

     var newProduct = new Product({ 
      name: params.name, 
      category_id: params.category_id, 
      description: params.description, 
      is_active: true 
     }); 

     newProduct.save(function (err, result) { 
      if(err) callback({ status: false, message: err }); 

      callback({ status: true, message: result }); 
     }); 
    }); 
}, 

Mein Ziel ist es, die Daten auf einmal zu verarbeiten. Und jetzt bin ich nicht sicher, ob ich es mit busboy erreichen kann.

Bitte helfen Sie mir dabei.

Bündel Dank im Voraus

Antwort

0

Sie für busboy warten sollte Ihre Form Parsen vor dem Aufruf Ihrer Methode zu beenden.

busboy.on('finish', function() { 
    productController.addNewProduct(fields, function (data) { 
    res.json(data); 
    }); 
}); 
0

Ich denke, ich habe bereits die Antwort gefunden. Danke an Marton für den Kopf.

Ich habe versucht:

busboy.on('finish', function() {}); 

aber nie emittiert.

Der Grund ist, ich muss file.resume() in meinem busboy.on ('Datei'), Beispiel:

busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { 
fields[fieldname] = file; 
file.resume(); 

Noch einmal danke :)