无法创建新的GridStore

时间:2015-03-11 17:44:04

标签: node.js mongodb mongoose gridfs

我正在使用Gridfs创建一个文件并将其保存在mongodb中。我正在使用console.log来确定错误的位置。我的网络应用程序允许用户创建文本文件,网络应用程序将其保存在mongodb中。为了保存文件,我正在使用Gridfs,但是我的程序没有通过行var gridStores = new GridStore (db, names, "w");。我似乎无法弄清楚为什么,我认为我的新GridStore肯定有问题。谢谢您的帮助。

saveFile.js:

var Files = require('C:/Users/OneDrive/workspace/Editor/Models/files.js');
var mongoose = require('mongoose');
var User = require('C:/Users/OneDrive/workspace/Editor/Models/users.js');
var db = mongoose.connection.db;
var GridStore = mongoose.mongo.GridStore;
var Grid = mongoose.mongo.Grid;
var SaveFile = function(names, contents){
console.log("inside SaveFile");
console.log("this is the name: " + names);
console.log("this is the content: " + contents);
var gridStores = new GridStore (db, names, "w");
console.log("pass new gridStore ");
gridStores.open(function(err, gridStores){
    console.log("inside open gridstor");
    gridStores.write(this.contents, function(err, gridStores){
        assert.equal(null, err);
        gridStores.close(function(err, result){
            assert.equal(null, err);
        });
    });
});
console.log("passed GridStore");
findorCreateUser();
}
findOrCreateUser = function(){
console.log("Inside Create user");
    User.findOne({'username' :  username },
        function(err, user) {
            if (err){
                console.log('Error in SignUp: '+err);
                return done(err);
            }
            if (user) {
                user.files = gridStore;
                user.save(function(err) {
                    if (err){
                        console.log('Error in Saving file:'+err);  
                        throw err;  
                    }
                    console.log('File Saved');    
                    return done(null, user);
                    });
                } 
            else {
                console.log('User already exists with username: '+username);
                return done(null, false, req.flash('message',
                    'User Already Exists'));
            }
        });
}
module.exports = SaveFile;

这是我的用户架构:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');
var SALT_WORK_FACTOR = 10;
var UserSchema = new Schema({
username:{type: String, required: true},
password:{type: String, required: true},
    files:{type: Schema.Types.Mixed, required: false}
}, {collection: 'Logins'});

UserSchema.pre('save', function(next) {
var user = this;
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
    mongoose.set('debug', true);
    if (err) 
        return next(err);
    bcrypt.hash(user.password, salt, null, function(err, hash) {
        if (err) 
            return callback(err, hash);
        user.password = hash;
        next();
    });

  });
});
module.exports = mongoose.model('Users', UserSchema);

0 个答案:

没有答案