我有一个'form',其中包含'file upload'。首先,我将文件上传到我的目录中的'uploads文件夹',然后我想保存所有表单数据,包括上传到MongoDb的文件路径。但我做错了,因为我收到以下错误:
无法读取未定义的属性'txtEmailorPhoneNumber' at Object.user.saveUser(/Users/juliatrefas/Desktop/WEBSHOP-NODE/user.js:23:40) 在app.post(/Users/juliatrefas/Desktop/WEBSHOP-NODE/server.js:49:10) 在Layer.handle [as handle_request](/Users/juliatrefas/Desktop/WEBSHOP-NODE/node_modules/express/lib/router/layer.js:95:5) 在下一个(/Users/juliatrefas/Desktop/WEBSHOP-NODE/node_modules/express/lib/router/route.js:137:13) 在Route.dispatch(/Users/juliatrefas/Desktop/WEBSHOP-NODE/node_modules/express/lib/router/route.js:112:3) 在Layer.handle [as handle_request](/Users/juliatrefas/Desktop/WEBSHOP-NODE/node_modules/express/lib/router/layer.js:95:5) at /Users/juliatrefas/Desktop/WEBSHOP-NODE/node_modules/express/lib/router/index.js:281:22 在Function.process_params(/Users/juliatrefas/Desktop/WEBSHOP-NODE/node_modules/express/lib/router/index.js:335:12) 在下一个(/Users/juliatrefas/Desktop/WEBSHOP-NODE/node_modules/express/lib/router/index.js:275:10) 在serveStatic(/Users/juliatrefas/Desktop/WEBSHOP-NODE/node_modules/serve-static/index.js:75:16)
这是我的server.js:
var http = require('http')
var express = require('express')
var app = express()
server = http.createServer(app)
var formidable = require('formidable');
//app.use(formidable());
app.use(express.static(__dirname + '/public'));
var fs = require('fs')
var path = require('path');
var mongo = require('mongodb').MongoClient
global.db = null
var sDatabasePath = 'mongodb://localhost:27017/webshop'
global.mongoId = require('mongodb').ObjectID
/**************************************************/
var user = require(path.join(__dirname + '/user.js'))
/**************************************************/
app.get('/shopaholic', (req, res) => { res.sendFile(path.join(__dirname + '/shopaholic.html')) })
mongo.connect(sDatabasePath, (err, db) => {
if (err) {
console.log('ERROR 003 -> Cannot connect to the database')
return false
}
global.db = db
console.log('OK 002 -> Connected to the database')
return true
})
/**************************************************/
app.post('/save-user', (req, res) => {
var form = new formidable.IncomingForm();
form.parse(req);
form.on('fileBegin', function (name, file) {
file.path = __dirname + '/uploads/' + file.name
global.sImagePath = file.path
});
form.on('file', function (name, file) {
console.log('Uploaded ' + file.name);
});
user.saveUser(req.fields, req.files, (err, jResult) => {
if (err) {
console.log(jResult)
return res.send('ERROR')
}
console.log(jResult)
return res.send('OK')
})
})
/**************************************************/
app.listen(3000, (err) => {
if (err) {
console.log('ERROR 001 -> Cannot listen to port 3000')
return false
}
console.log('OK 000 -> Server listening to port 3000')
})
我的user.js:
var user = {}
/**************************************************/
user.saveUser = (jUserData, fcallback) => {
var jUser = { "userName": jUserData.txtEmailorPhoneNumber, "firstName": jUserData.txtName, "lastName": jUserData.txtLastName, "password": jUserData.txtPassword, "image": jUserData.global.sImagePath }
global.db.collection('users').insertOne(jUser, (err, jResult) => {
if (err) {
var jError = { "status": "error", "message": "ERROR -> user.js -> 001" }
console.log(jError)
return fcallback(true, jError)
}
var jOk = { "status": "ok", "message": "user.js -> saved -> 000" }
console.log(jOk)
return fcallback(false, jOk)
})
}
/**************************************************/
module.exports = user