【问题标题】:How to get id back, after posting data to mongoDb?将数据发布到 mongoDb 后如何取回 id?
【发布时间】:2017-08-30 06:06:31
【问题描述】:

我正在使用 node.js、angularjs 和 mongoDb。
我正在创建一个产品上传页面。

分为两部分:

  1. 数据页面:这部分将包含文本字段和下拉菜单。
  2. 图片上传页面:这部分会有图片上传控制。

所以我想在同一页面创建2个表单,从第一页开始我将文本数据发布到mongoDb,返回新创建的产品的product_id,然后上传返回的product_id图片

我开发了restFul API 来发布产品api/products/create-product
产品型号

{
productName:{type: String},
productPrice:{type: Number}
}


图像模型

{
productId:{type: String},
imagePaths:[{type: Array}]
}


产品控制器(Angular):

$scope.newProduct = function(){
      var formData = new FormData;
      for(key in $scope.product){
        formData.append(key, $scope.product[key]);
      } 
  //getting the files
  var file = $('#file')[0].files[0];
  formData.append('image', file);

  //Post data
  $http.post('http://localhost:3000/products/api/new-product',formData,{
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined}
  }).then(function(res){
    $scope.item = res.data;

  });
}


Angular 前端

<input type="text" class="form-control" ng-model="product.productName" placeholder="Enter Product Name">
<input type="file" multiple="multiple" id="file" >
<button type="submit" ng-click="newProduct()" class="btn btn-primary">Add Product</button>


POST API

router.post('/api/new-product',upload.any(),function(req, res, next){

  var pro = req.body;
  if(req.files){
    req.files.forEach(function(file){

      var filename = (new Date()).valueOf() + '-' + file.originalname;
      fs.rename(file.path,'public/images/'+ filename, function(err){
        if (err) throw err;
        //Save to mongoose

      var product = new Products({
          productName: req.body.productName
         });
          product.save(function(err, result){
          if(err){ throw err}
            res.json(result);
        });



      });
    });
  }
});

问题

  1. 我这样做是正确的,还是有其他更好的方法?
  2. 如果这是正确的方法,那么我怎样才能发布获得发布的 product_id,以便发布图片?
    谢谢。

【问题讨论】:

  • 您也可以在您的客户端代码中插入之前生成一个新的ID。这样,您就不需要获取新的 id,您已经知道了。
  • 如何在将数据推送到 mongoDb 之前创建 productId
  • 不知道如何在 node.js 中执行此操作。我敢肯定,文档已经涵盖了它。
  • 我相信,当您在集合中插入新文档时,mondoDb 会为您的条目创建一个唯一 id。因此,在将任何内容推送到数据库之前,我们无法创建唯一键。
  • 这是默认行为,是的。但是 mongo 的对象 id 的生成方案提供了很好的保证,您可以在客户端生成它并且它仍然是唯一的。

标签: angularjs node.js mongodb express


【解决方案1】:

5 秒进入谷歌(未测试):

collection.insert(objectToInsert, function(err){
   if (err) return;

   // Object inserted successfully.
   var objectId = objectToInsert._id; // this will return the id of object inserted
});

Source

【讨论】:

    【解决方案2】:

    这是我与 mongodb 连接的配置文件。这是config.js

    module.exports = {
        'secretKey': '12345-67890-09876-54321',
        'mongoUrl' : 'mongodb://localhost:27017/image'
    }
    

    这是我的schema's。我创建了两个集合,一个是products,另一个是images。将这两个模式保存在 models 文件夹中。这是我的产品架构,我将其命名为product.js

    var mongoose = require('mongoose');
    
    var nameSchema = new mongoose.Schema({
    
    productName:{type: String},
    productPrice:{type: Number}
    
    });
    module.exports  = mongoose.model("product", nameSchema);
    

    这是我的图像架构,我将其命名为 image.js

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    var imageSchema = new Schema({
    imagepath:{
        type:String,
        required:true
    }
    });
    var nameSchema = new Schema({
     productId:{type: String},
    imagePaths:[imageSchema]
    });
    module.exports  = mongoose.model("image", nameSchema);
    

    这是html 文件,将此文件保存在views 文件夹中。我将其命名为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>
    

    接下来是路由文件,将此文件保存在routes文件夹中并命名为route.js

    var express = require('express');
    var bodyParser = require('body-parser');
    var mongoose = require('mongoose');
    
    var Image = require('../models/image');
    var Product = require('../models/product');
    var app = express();
    var Router = express.Router();
    Router.use(bodyParser.json());
    
    Router.get('/product',function(req,res){
     Product.find({}, function (err, product) {
            if (err) throw err;
            res.json(product);
        });
    })  
    Router.post('/productData',function(req, res, next){
        Product.create(req.body, function (err, product) {
            if (err) throw err;
            console.log('Product Data created!');
            var id = product._id;
    
            res.writeHead(200, {
                'Content-Type': 'text/plain'
            });
            res.end('Added the product data with id: ' + id);
        });    
    })
    Router.put('/postingImage/:Id',function(req,res,next){
     Image.findByIdAndUpdate(req.params.Id, {
            $set: req.body
        }, {
            new: true
        }, function (err, batch) {
            if (err) throw err;
            res.json(batch);
        });
    })
    
    Router.get('/image',function(req,res){
     Image.find({}, function (err, img) {
            if (err) throw err;
            res.json(img);
        });
    })  
        module.exports = Router;
    

    这里是服务器代码,命名为app.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:/syed ayesha/nodejs/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");
    });
    

    node app.js 运行服务器

    这是我的API's 在 mongodb 中发布产品详细信息和发布图片路径

    1. 使用POST 方法发布产品详细信息使用http://localhost:3000/route/productData。通过像
    2. 这样的正文发布数据

    { “产品名称”:“奶油”, “产品价格”:88 }

    1. 使用GET 方法从mongodb 获取产品详细信息,用于http://localhost:3000/route/product

    2. 现在打开浏览器并输入http://localhost:3000/api/file 然后选择要上传的文件单击提交按钮然后您将获得文档 ID 作为响应。只需记下此 ID。您将使用此 I 发布 productId图像架构。

    3. 当您想从 mongodb 中查看图像路径详细信息时,请使用 GET 方法并使用 http://localhost:3000/route/image

    4. 现在您可以使用之前获得的文档 ID 在图像架构中添加 productId。为此使用PUT 方法并在此处使用http://localhost:3000/route/postingImage/59ae2f9195730f1e00be7509 我只是给了我的文档ID。您需要将文档ID 放在那里。并像这样通过正文发送productId

      { “productId”:“59a6ac68a87d9f102c4496b8” }

    在此之后,您将回复为

    你也可以在 mongodb 中 che。

    1. use image
    2. show collections
    3. db.images.find().pretty();
    4. db.product.find().pretty(); 希望这会有所帮助。

    【讨论】:

    • 感谢您的回答,真的很有帮助。
    • 你知道有哪些在线编辑器可以练习 MEAN 堆栈吗?
    • 我很高兴它有帮助...如果您有任何疑问可以问我,我会尽力解决...
    • 当然,这真的很有帮助:)
    • 我没有使用任何在线编辑器我只是使用邮递员工具来执行应用程序......
    【解决方案3】:

    您可以使用此代码发布产品数据

    app.post('/productData',function(req, res, next){
        Product.create(req.body, function (err, product) {
            if (err) throw err;
            console.log('Product Data created!');
            var id = product._id;
    
            res.writeHead(200, {
                'Content-Type': 'text/plain'
            });
            res.end('Added the product data with id: ' + id);
        });    
    })
    

    以同样的方式,您可以使用 product_id 发布图像,当产品添加到 mongodb 时,该产品将作为响应。当您想查看它所属的图像时,您可以将 product_id 作为参数传递

    app.get('/productImage/:productId',function(req,res,next){
    Image.find({"product_id":req.params.productId}, function (err, data) {
        if(err) console.log("error");
        if(data==true){
            res.json(batch);
        }
        });
    });
    

    如果您需要任何其他信息,请告诉我。希望这会有所帮助

    【讨论】:

    • 好的,我会尝试这种方法。您能帮我将多张图片上传到服务器文件夹,并将 imagePaths 保存到 mongoDb 吗?
    • 我搜索了很多,但找不到任何可行的解决方案。我得到了将使用 multer 等模块的教程,但它们只会将图像保存到文件夹,但不会将 imagePaths 保存到 mongoose 集合中。
    • 非常感谢,我已经有一个代码 sn-p,它会将一个 imagePath 发布到 mongoDb,我需要修复该代码,以便我可以将 multiplt imapePaths 发布到图像集合。我会分享那个代码也和你一起。
    • 请务必更新您的帖子,以便对所有人有所帮助
    • 你想要上传文件的路径还是上传文件的目标路径???
    猜你喜欢
    • 2018-11-27
    • 2018-01-07
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多