0

我想用上传文件的路径更新我的 MongoDb 数据库。我正在使用 multer 中间件进行文件上传。但是在我的例子中,$push 并没有向数组推送任何值。

onFileUploadComplete: function (file) {
    cs.update(
        {'question.qid' : pprcd+qusid},
        {$push: { 'question.images.image' : file.path} },
        function(err){
            if(err){
                console.log(err);
            }else{
                console.log('Pushed filePath to db');
            }
        }
    );
     console.log(file.fieldname + ' uploaded to  ' + file.path);
}

我的代码有什么问题?假设 pprcd 和 qusid 是两个具有一定值的预定义全局变量。

4

1 回答 1

0

尝试在发布路线内更新。这条路线是从表单中调用的。`

html
      head
        title File upload Node.
      body
        form#uploadForm(enctype='multipart/form-data', action='/api/photo', method='post')
          input(type='file', name='userPhoto')
          input(type='submit', value='Upload Image', name='submit')
      script(src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js')
      script(src='http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js')
      script.
        $(document).ready(function() {
        $('#uploadForm').submit(function() {
        $(this).ajaxSubmit({
        error: function(xhr) {
        status('Error: ' + xhr.status);
        },
        success: function(response) {
        console.log(response);
        }
        });
        return false;
        });
        });
---------------------------ur sever file-------------
    app.use(multer({ dest: './uploads/',
     rename: function (fieldname, filename) {
        return filename+Date.now();
      },
    onFileUploadStart: function (file) {
      console.log(file.originalname + ' is starting ...')
    },
    onFileUploadComplete: function (file) {
      console.log(file.fieldname + ' uploaded to  ' + file.path)
      done=true;
    }
    }));

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

    app.post('/api/photo',function(req,res){
      if(done==true){
        console.log(req.files);
        res.end("File uploaded.");
      }
    });
于 2015-07-09T08:25:51.497 回答