Mongoose Meanjs无法将图像保存到嵌套架构中

Mongoose Meanjs无法将图像保存到嵌套架构中,mongoose,mean,meanjs,Mongoose,Mean,Meanjs,我的Meanjs应用程序应该将徽标上传到我的UserSchema中的嵌套模式中。我能够成功上传文件,因为我能够为用户成功存储个人资料照片。但是,当我试图将图像保存到嵌套模式中时,它失败了 我的模型看起来像这样: ClubSchema = {name:String, logo:Buffer}; UserSchema = {name:String, photo:Buffer, club:[ClubSchema]

我的Meanjs应用程序应该将徽标上传到我的UserSchema中的嵌套模式中。我能够成功上传文件,因为我能够为用户成功存储个人资料照片。但是,当我试图将图像保存到嵌套模式中时,它失败了

我的模型看起来像这样:

ClubSchema = {name:String,
              logo:Buffer};

UserSchema = {name:String,
              photo:Buffer,
              club:[ClubSchema]
             }
我能够成功地将所有数据插入我的ClubSchema,但无法插入徽标

My client.controller.js

    var user = new Users($scope.user);
    console.dir(user.club);      //I can see the logo here
    for(var i = 0;i<user.club.length;i++){
        user.club[i].logo = $scope.uploadedLogos[i];  //Still trying to force it into the object

        user.club[i].hasLogo = true;    //Mysterious hasLogo = false appears
    }
    user.$update(function(response) {
        $scope.success = true;
        console.log('Inside scope success User is ');
        console.dir(user);               //hasLogo=false appears in this and no logo
        $location.path('/');
        }, function(response) {
             $scope.error = response.data.message;
        });
if (user) {
    // Merge existing user
    user = _.extend(user, req.body);
    user.updated = Date.now();
    user.displayName = user.name;

    user.save(function(err) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            req.login(user, function(err) {
                if (err) {
                    res.status(400).send(err);
                } else {
                    res.json(user);
                }
            });
        }
    });
} else {
    res.status(400).send({
        message: 'User is not signed in'
    });
}

hasLogo字段完全令人困惑,因为我的代码中不存在这样的赋值。我添加了hasLogo=true,希望它能以某种方式将hasLogo设置为true,然后上传图像,但这也不起作用。我不知道这个hasLogo是从哪里来的。在hasLogo=true循环之后,如果打印对象,我可以看到两个hasLogo字段,一个为true,另一个为false。在$update成功之后,它总是false。我的模型不包含hasLogo字段。

这是我使用MEAN.JS上传文件的方式

型号

  var UserSchema = new mongoose.Schema({
  name:{type:String,required:true},
  photo:Buffer  // Image
  });
<input ng-file-select="onAttachmentSelect($files)" ng-model="getFileData" name="upload" type="file" required="true">
服务器控制器

var userPicture = function(req,res){             // Stores Picture for a user matching the ID.
    user.findById(req.param('id'), function (err, user) {
        console.log(req.files) // File from Client
        if(req.files.file){   // If the Image exists
            var fs = require('node-fs');
            fs.readFile(req.files.file.path, function (dataErr, data) {
                if(data) {
                    user.photo ='';
                    user.photo = data;  // Assigns the image to the path.
                    user.save(function (saveerr, saveuser) {
                        if (saveerr) {
                            throw saveerr;
                        }
                        res.json(HttpStatus.OK, saveuser);                        
                    });
                }
            });
            return
        }
        res.json(HttpStatus.BAD_REQUEST,{error:"Error in file upload"});
    });
};
    $scope.saveuserImage =  function(){
        $scope.upload = $upload.upload({  // Using $upload
            url: '/user/'+$stateParams.id+'/userImage',  // Direct Server Call.
            method:'put',
            data:'',  // Where the image is going to be set.
            file: $scope.file
        }).progress(function (evt) {})
            .success(function () {
                var logo = new FileReader();  // FileReader.

                $scope.onAttachmentSelect = function(file){
                    logo.onload = function (e) {
                        $scope.image = e.target.result;  // Assigns the image on the $scope variable.
                        $scope.logoName = file[0].name; // Assigns the file name.
                        $scope.$apply();
                    };
                    logo.readAsDataURL(file[0]);
                    $scope.file = file[0];
                    $scope.getFileData = file[0].name
                };
                location.reload();
                $scope.file = "";
                $scope.hideUpload = 'true'
            });
        $scope.getFileData = '';
     //        location.reload()
    };
客户端控制器

var userPicture = function(req,res){             // Stores Picture for a user matching the ID.
    user.findById(req.param('id'), function (err, user) {
        console.log(req.files) // File from Client
        if(req.files.file){   // If the Image exists
            var fs = require('node-fs');
            fs.readFile(req.files.file.path, function (dataErr, data) {
                if(data) {
                    user.photo ='';
                    user.photo = data;  // Assigns the image to the path.
                    user.save(function (saveerr, saveuser) {
                        if (saveerr) {
                            throw saveerr;
                        }
                        res.json(HttpStatus.OK, saveuser);                        
                    });
                }
            });
            return
        }
        res.json(HttpStatus.BAD_REQUEST,{error:"Error in file upload"});
    });
};
    $scope.saveuserImage =  function(){
        $scope.upload = $upload.upload({  // Using $upload
            url: '/user/'+$stateParams.id+'/userImage',  // Direct Server Call.
            method:'put',
            data:'',  // Where the image is going to be set.
            file: $scope.file
        }).progress(function (evt) {})
            .success(function () {
                var logo = new FileReader();  // FileReader.

                $scope.onAttachmentSelect = function(file){
                    logo.onload = function (e) {
                        $scope.image = e.target.result;  // Assigns the image on the $scope variable.
                        $scope.logoName = file[0].name; // Assigns the file name.
                        $scope.$apply();
                    };
                    logo.readAsDataURL(file[0]);
                    $scope.file = file[0];
                    $scope.getFileData = file[0].name
                };
                location.reload();
                $scope.file = "";
                $scope.hideUpload = 'true'
            });
        $scope.getFileData = '';
     //        location.reload()
    };
Html

  var UserSchema = new mongoose.Schema({
  name:{type:String,required:true},
  photo:Buffer  // Image
  });
<input ng-file-select="onAttachmentSelect($files)" ng-model="getFileData" name="upload" type="file" required="true">

ng file select用于从客户端获取文件


这对我来说很好。希望这有帮助

user.club[i].徽标就是徽标所在的位置,对吗。。?$scope.uploadedLogos[i]有什么?$scope.uploadedLogos[i]只是我创建的一个单独的数组,用于获取正在上载的徽标的辅助存储,然后将它们重新分配给$scope.user.club[i]。logo,以防它被删除。没有帮助,因为它仍然在我试图更新的最终用户对象中被删除。与最终用户对象没有实际关联。只是我在尝试的东西。我试图使用$scope.user更新的最后一个var用户仍然不包含徽标您从客户端获得的徽标是这种格式的吗?“数据:图像/png;base64,Ivborw0kgoaansuheugaac4aaahocamaamobmcaaaam1bmveuyicg9j8pjlcxum85godbrp9j3rdscs9nudizv9ukxd2wy9+70eh1+PS3eXe4+fp6elaxdwaaamg0leqvr42u3d7qkgyf4aiuyra/ed7fvvt96e7te3jkjkjkjkjkjkjkj4cwkj4cwkjkjjkjkjkjkjkjkjkjkjkjkjkjkjkjkjkwkjkjkjkjkjkjkjkjkwkjkjkjkjkjkjkwg8wg8wkjkjkjkjkjkjkjjjINGRNOAIEBK8BKJVHCMTMY4H18ZX7IKVODSYGHGNYZL/ZXTIYLHVFGDJ8TXKXZOGUBU4NSAV8YRHMS5RHLNVTCOZZLOKGBIWL7KIYCLWTL4TL0MUY9S9ANECF=”是的,我正在使用ng file upload指令上载文件。用户的照片上传完全使用此指令。顺便说一句,我找到了一个解决方案,将Club作为一个单独的模块,并与用户模型分开创建/更新它。不确定这是否是正确的方法,但它是有效的。我很想知道最初的实现有什么问题。我已经能够成功上传图片了。问题在于将其插入mongodb文档中的嵌套字段中。