如何使用节点api在mongoose中保存图像?

时间:2015-11-07 17:50:50

标签: node.js mongodb mongoose

以下是我的模特

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;


var dummySchema = new Schema({
    name: {
        type: String,
        required: true
    },
    brand: {
        type: String
    },
    class: {
        type: String,
        required: true
    },
    price: {
        type: Number,
        required: true
    },
    available: {
        type: Boolean,
        required: true
    },

    dateavailable: {
        type: Date,
        default:Date.now    },
    avatar:{
        data:Buffer,
        contentType:String

    }
});



module.exports = mongoose.model('Dummy', dummySchema);

保存新实体的快速路线位于

之下
app.post('/api/cars', function(req, res) {

        var dummy = new Dummy();
        dummy.name = req.body.name;
        dummy.brand = req.body.brand;
        dummy.class = req.body.class;
        dummy.price = req.body.price;
        dummy.available = true;
        dummy.avatar.data = fs.readFileSync(req.body.avatar);
        dummy.avatar.contentType="png";



        dummy.save(function(err) {
            if (err)
                res.send(err);
            res.send("sucess");

        });
    });

当我试图使用邮递员通过http / post插入数据时,如下所示

Screen shot from Postman

我收到以下错误

Error: ENOENT: no such file or directory, open 'E:\Fall_2015\SE\car_rental_software\server\https:\drive.google.com\file\d\0B9YLBjJ0zJ6YWDB4NUlFUThVS0E\view?usp=sharing'
<br> &nbsp; &nbsp;at Error (native)
<br> &nbsp; &nbsp;at Object.fs.openSync (fs.js:549:18)
<br> &nbsp; &nbsp;at Object.fs.readFileSync (fs.js:397:15)
<br> &nbsp; &nbsp;at E:\Fall_2015\SE\car_rental_software\server\index.js:88:32
<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (E:\Fall_2015\SE\car_rental_software\server\node_modules\express\lib\router\layer.js:95:5)
<br> &nbsp; &nbsp;at next (E:\Fall_2015\SE\car_rental_software\server\node_modules\express\lib\router\route.js:131:13)
<br> &nbsp; &nbsp;at Route.dispatch (E:\Fall_2015\SE\car_rental_software\server\node_modules\express\lib\router\route.js:112:3)
<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (E:\Fall_2015\SE\car_rental_software\server\node_modules\express\lib\router\layer.js:95:5)
<br> &nbsp; &nbsp;at E:\Fall_2015\SE\car_rental_software\server\node_modules\express\lib\router\index.js:277:22
<br> &nbsp; &nbsp;at Function.process_params (E:\Fall_2015\SE\car_rental_software\server\node_modules\express\lib\router\index.js:330:12)
<br> &nbsp; &nbsp;at next (E:\Fall_2015\SE\car_rental_software\server\node_modules\express\lib\router\index.js:271:10)
<br> &nbsp; &nbsp;at methodOverride (E:\Fall_2015\SE\car_rental_software\server\node_modules\method-override\index.js:79:5)
<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (E:\Fall_2015\SE\car_rental_software\server\node_modules\express\lib\router\layer.js:95:5)
<br> &nbsp; &nbsp;at trim_prefix (E:\Fall_2015\SE\car_rental_software\server\node_modules\express\lib\router\index.js:312:13)
<br> &nbsp; &nbsp;at E:\Fall_2015\SE\car_rental_software\server\node_modules\express\lib\router\index.js:280:7
<br> &nbsp; &nbsp;at Function.process_params (E:\Fall_2015\SE\car_rental_software\server\node_modules\express\lib\router\index.js:330:12)

有人能告诉我如何在Mongoose中使用Node API保存图像吗?

0 个答案:

没有答案