我有這個MEAN應用程序,用戶可以在其中上傳他們的照片。這裏是路由爲:Node.js/mongoose:undefined不是函數
var express = require('express');
var router = express.Router();
var passport = require('passport');
var User = require('../models/user');
var Verify = require('./verify');
var multiPart = require('connect-multiparty');
var multiPartMiddleWare = new multiPart();
var fs = require('fs-extra');
var path = require('path');
router.post('/edit', multiPartMiddleWare ,function(req, res) {
var file = req.files.file;
theuser = req.body.username;
console.log("User " + theuser + "..." + file);
var uploadDate = new Date();
console.log(file.path + '\n')
var tempPath = file.path;
var finalPath = path.join(__dirname, "../userphotos/", theuser + file.name);
console.log(finalPath);
var savePath = "/userphotos/" + theuser + uploadDate + file.name;
fs.rename(tempPath, finalPath, function (err){
if (err){
console.log(err)
} else {
User.find({"username":theuser}, function(err, userData){
var userd = userData;
userd.image = savePath;
console.log('\n' + userd + '\n'); // the userd is proper
userd.save(function(err){ //Error occurs HERE
if (err){
console.log("Error whilst saving file: " + err)
res.json({status: 500})
} else {
console.log("Image saved");
res.json({status: 200})
}
})
})
}
})
});
圖像被保存到我的臨時文件夾以及目的地。但是,當我嘗試將源代碼保存到用戶文檔時,我得到一個undefined is not a function
。
這裏是我的貓鼬路線:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');
var User = new Schema({
username: {
type: String,
unique: true
},
password: String,
image: {
type: String
},
bio: {
type: String
}
});
User.plugin(passportLocalMongoose);
module.exports = mongoose.model('User', User);
請問該如何解決這個問題?