【问题标题】:Adding another image path in mongodb using nodejs使用nodejs在mongodb中添加另一个图像路径
【发布时间】:2018-02-13 07:18:57
【问题描述】:

我可以在 mongodb 中存储图像路径。这里我以数组格式存储图像路径。使用文档 ID 我需要添加另一个图像,即我想将另一个图像路径推送到该数组中。所以我的问题是如何在 mongodb 中存储另一个图像路径。

我在这里使用html 文件上传图片。这是我的代码index.html

<form id="uploadForm"
      enctype="multipart/form-data"
      action="/api/file"
      method="post"
>
 <input type="file" name="userFile"/>
<input type="submit" value="Upload File" name="submit">

</form>

这是我的服务器代码server.js

var express=require('express');
var multer=require('multer');
var bodyParser = require('body-parser');
var Image=require('./models/image');
var Product=require('./models/product');
var mongoose=require('mongoose');
var path = require('path');
var rand;
var urlencodedParser = bodyParser.urlencoded({ extended: false });

var config = require('./config');

mongoose.connect(config.mongoUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
    console.log("Connected correctly to server");
});
var app=express();
var ejs = require('ejs')
app.set('view engine', 'ejs')
var storage = multer.diskStorage({
    destination: function(req, file, callback) {
        callback(null, './public/uploads')
    },
    filename: function(req, file, callback) {
        //callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
                //callback(null, file.originalname)
        rand=Date.now() + path.extname(file.originalname);

        callback(null, file.fieldname + '-' + rand);

    }

})
var upload = multer({
        storage: storage});
app.get('/api/file',function(req,res){
res.sendFile('E:/saas/nodejs/uploads/db/views/index.html');
});

app.post('/api/file',upload.single('userFile'), function(req, res) {
    console.log(req.file);
    console.log(req.file.path);

    Image.create({imagePaths:[{imagepath:req.file.path}]},function(err,img){

            if (err) throw err;
     console.log(img);
        console.log('Path created!');
        var id = img._id;

        res.writeHead(200, {
            'Content-Type': 'text/plain'
        });
        res.end('Added the image path with id: ' + id);
    });    
})

var route=require('./routes/route');
app.use('/route',route);
    app.listen(3000,function(){
    console.log("Server listening on 3000");
});

运行服务器后,我将在浏览器http://localhost:3000/api/file 中使用它,使用它我可以上传文件,我将获得 mongodb 文档 ID 作为响应。使用此 ID 如何上传另一个图像路径。

【问题讨论】:

  • @Dinesh 在这里我的服务器代码中的路由 /api/file 与 html 表单中的操作链接。有了这个我可以上传。它与 html 表单链接。那么我怎样才能用另一条路由推送route 应该找到 id 并且必须制作上传文件
  • 在路由中将 id 设为可选。在服务器端检查路由中的 id 是否存在,使用 $push 更新旧的 mongodb 文档。其他明智的创建新的。要使其成为可选,请参阅此处stackoverflow.com/questions/10020099/…,而不是一次单个文件,为什么不能一次上传所有图像?。
  • @dinesh 我试过了..这个概念适用于 get 方法。当我在 get 方法路由和 post 方法路由中使用可选参数更改路由时。没有变化。图像添加为新的文档。请你告诉我如何在我的 post 方法中做到这一点

标签: javascript html node.js mongodb image


【解决方案1】:

使用$push将路径存入数组

schemaName.findByIdAndUpdate(
   { req.headers.id },
   { $push: { imagePath: '/newPath' } },
   (err,response)=>{
        if(err){
             console.log(err);
        }

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 2017-11-19
    相关资源
    最近更新 更多