Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

javascript - Mongo chunks not written

I am using MongoDB, nodeJS, express, multer, jimp and gridFS to upload an image. I first uploaded the image with multer then resized it with jimp then uploaded the jimp buffer to MongoDB now when I delete the multer image an error comes that the file is probably corrupt as the chunks file is not written(i don't know why but it is actually not written when I want to delete it) can any one tell how to achieve this and what is the error in my code.

here is my post route

app.post('/upload', upload.single("file"), (req, res) => {


if(req.file === undefined || req.file === 0 || req.file === ""){
  res.redirect("/");
}
console.log(req.file);
filename = req.file.filename;
Jimp.read( "http://localhost:3000/image/" + req.file.filename, (err, image) => {
  if (err) {
    console.log(err);
  }
    // .then(lenna => (tpl.clone().write(imgActive)))
  image
    .resize(500, Jimp.AUTO)
      // console.log(img);
      image.getBase64(Jimp.AUTO, (error1, base64Image) => {
        if(error1){
          console.log(error1);
        }
        const image1 = new Image({
          image: base64Image,
          User: "Avichal",
          forTest: "Hindi1"
        });

        image1.save(function(error){
          if(error){
            console.log(error);
          }
           })

    })
})

gfs.remove({ _id: req.file.id, root: 'uploads' }, (err, gridStore) => {
  if (err) {
    console.log(err);
  }

});


  res.redirect('/');
});

here is the error: MongoError: no chunks found for file, possibly corrupt


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

just a small error I have to put the remove function In the jimp read function the corrected code is:

app.post('/upload', upload.single("file"), (req, res) => {


if(req.file === undefined || req.file === 0 || req.file === ""){
  res.redirect("/");
}
console.log(req.file);
filename = req.file.filename;
Jimp.read( "http://localhost:3000/image/" + req.file.filename, (err, image) => {
  if (err) {
    console.log(err);
  }
    // .then(lenna => (tpl.clone().write(imgActive)))
  image
    .resize(500, Jimp.AUTO)
      // console.log(img);
      image.getBase64(Jimp.AUTO, (error1, base64Image) => {
        if(error1){
          console.log(error1);
        }
        const image1 = new Image({
          image: base64Image,
          User: "Avichal",
          forTest: "Hindi1"
        });

        image1.save(function(error){
          if(error){
            console.log(error);
          }
           })

    })
gfs.remove({ _id: req.file.id, root: 'uploads' }, (err, gridStore) => {
  if (err) {
    console.log(err);
  }
  res.redirect('/');
});
})




});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...