/ / Multer 1.0.3 Przesyłanie pliku w formacie Mean Stack - angularjs, node.js, dane wieloczęściowe, multer

Multer 1.0.3 Przesyłanie pliku w formacie Mean Stack - angularjs, node.js, dane wieloczęściowe, multer

Próbuję przesłać wieloczęściowe / formularzowe dane dla prostej aplikacji stosu MEAN. Podczas umieszczania wszystkiego w jednym pliku i uruchamiania go działa dobrze.

Server.js

var express = require("express");
var multer  = require("multer");
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads/");
},
filename: function (req, file, cb) {
switch(file.mimetype) {
case "image/jpg" :
case "image/jpeg":
case "image/gif":
var extension = file.mimetype.split("/");
extension = extension[extension.length-1];
break
case "video/quicktime":
var extension = "mov";
break
case "video/mp4":
var extension = "mp4";
break
default:
var extension = "jpeg";

}
cb(null, file.fieldname + "-" + Date.now() + "." + extension);
}
});
var upload = multer({ storage: storage });
var path    = require("path");
var app = express();

var segmentUpload = upload.fields([{ name: "segmentVideo", maxCount: 1}, { name: "segmentStill", maxCount: 1}, { name: "segmentGif", maxCount: 1}])
app.post("/photos/upload", segmentUpload, function (req, res, next) {
console.log(req);
console.log(req.files["segmentVideo"][0]);
console.log(req.files["segmentStill"][0]);
console.log(req.files["segmentGif"][0]);
console.log(req.body.title);
});

app.get("/", function(req, res) {
res.sendfile("./index.html");
});

app.listen(8080);
console.log("App listening on port 8080");

index.html

<body>
<form action="/photos/upload" method="post" enctype="multipart/form-data">
title: <input type="text" name="title"> <br><br>
Select video to upload:
<input type="file" name="segmentVideo" id="fileToUpload"> <br><br>
Select jpeg to upload:
<input type="file" name="segmentStill" id="fileToUpload"><br><br>
Select gif to upload:
<input type="file" name="segmentGif" id="fileToUpload"><br><br>
<input type="submit" value="Upload Image" name="submit">
</form>

Ale kiedy próbuję zintegrować multer z routingiem w węźle, nie mogę dostać aplikacji akceptującej wieloczęściowe / formularzowe dane.

app / routes.js

var Video   = require("./models/video");
var multer          = require("multer");
var storage         = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads/");
},
filename: function (req, file, cb) {
switch(file.mimetype) {
case "image/jpg" :
case "image/jpeg":
case "image/gif":
var extension = file.mimetype.split("/");
extension = extension[extension.length-1];
break
case "video/quicktime":
var extension = "mov";
break
case "video/mp4":
var extension = "mp4";
break
default:
var extension = "jpeg";

}
console.log("new extension: " + extension);
cb(null, file.fieldname + "-" + Date.now() + "." + extension);
}
});
var upload          = multer({ storage: storage });

module.exports = function(app) {

// api --------------------------------------
// get all todos
app.get("/api/videos", function(req,res){

// use mongoose to get all todos in the database
Video.find(function(err, videos){

// if there is an error, send the error
if (err)
res.send(err);

res.json(videos);
});
});

var segmentUpload = upload.fields([{ name: "segmentVideo", maxCount: 1}, { name: "segmentStill", maxCount: 1}, { name: "segmentGif", maxCount: 1}])
app.post("/api/videos", segmentUpload, function(req, res){
// create a video
Video.create({
title : req.body.title,
description : req.body.description,
category : req.body.category,
day : req.body.day,
videoUrl : req.body.videoUrl,
stillUrl : req.body.stillUrl,
gifUrl : req.body.gifUrl,
airReady : false
}, function(err, video) {
if (err)
res.send(err)

// get and return all the todos after you create another
Video.find(function(err, videos){
if (err)
res.send(err)
res.json(videos);
});
});

});

// delete a todo
app.delete("/api/videos/:video_id", function(req,res){
Video.remove({
_id : req.params.video_id
}, function(err, video) {
if (err)
res.send(err);

// get and return all the todos after you delete one
Video.find(function(err, videos) {
if (err)
res.send(err)
res.json(videos);
});
});

});

// edit a todo
app.put("/api/videos/:video_id", function(req,res){
Video.findByIdAndUpdate(req.params.video_id,
{ $set: { title: req.body.title
}},
function(err, video){

if (err)
res.send(err)

// get and return all todos after edit
Video.find(function(err, videos){
if (err)
res.send(err)
res.json(videos);
});
});
});

// find -------------------------------------------------------------
app.get("*", function(req, res) {
res.sendfile("./public/index.html"); // load the single view file (angular will handle the page changes on the front-end)
});
};

Pojawia się błąd:

TypeError: Cannot read property &#39;title&#39; of undefined

To jest mój pierwszy post do przepełnienia stosu, ale doceniam całe wsparcie!

Odpowiedzi:

0 dla odpowiedzi № 1

Problem może wynikać z użycia wysyłającego multipart/form-data ale nie zamieniasz go na json i używasz json do pobierania danych.

Posługiwać się body-parser

var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.json());