Angular Express 将 json 添加到 mongoDB 中



我有一个json,我想直接添加到我的本地mongoDB中。

型:

var mongoose = require('mongoose');
var ObjectsSchema = new mongoose.Schema({
o_id: String,
details: Object
});
module.exports = mongoose.model('Objects', ObjectsSchema);

服务器代码 :

// set up ======================================================================
var express = require('express');
var app = express();                        // create our app w/ express
var mongoose = require('mongoose');                 // mongoose for mongodb
var port = process.env.PORT || 8080;                // set the port
var database = require('./config/database');            // load the database config
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
// configuration ===============================================================
mongoose.connect(database.localUrl);    
app.use(express.static(__dirname + '/app'));        // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json
app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Authorization, X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Session");
next();
});
// routes ======================================================================
require('./app/routes.js')(app);
// listen (start app with node server.js) ======================================
app.listen(port);
console.log("App listening on port " + port);

路线.js

var object = require('./model/Objects.model.js');
module.exports = function(app) {
app.post('/api/objects', function(req, res) {
    console.log(req.body);
    object.create({
        o_id: req.body.id,
        details: req.body.attributes
    }, function(err, todo) {
        if (err)
            res.send(err);
    });
});};

要求正文 :

[
{
  "id": "lLLLL",
  "attributes": {
    "Name": "Presentation Template, A.1",
    "Type": "Document"
  }
},
{
  "id": "2LLLL",
  "attributes": {
    "Name": "Presentation Template, A.2",
    "Type": "Document"
  }
},..............
]

在mongoDB中,当我做db.objects.find()时,会添加一个元素,但我找不到我真正添加到数据库中的内容。

该元素如下所示: { "_id" : ObjectId("574da9ac7dee7e001da0ba25"), "__v" : 0 }

谢谢

我对路线进行了以下更改.js

details: req.body

所以文件看起来像

var object = require('./model/model.js');
module.exports = function(app) {
app.post('/api/objects', function(req, res) {
    console.log(req);
    object.create({
        o_id: req.body.id,
        details: req.body
    }, function(err, todo) {
        if (err)
            res.send(err);
    });
});};

我使用高级休息客户端传入了以下正文

[
{
  "id": "lLLLL",
  "attributes": {
    "Name": "Presentation Template, A.1",
    "Type": "Document"
  }
},
{
  "id": "2LLLL",
  "attributes": {
    "Name": "Presentation Template, A.2",
    "Type": "Document"
  }
}
]

这就是进入集合的内容:

   db.objects.find().pretty()
{
        "_id" : ObjectId("574ec4396fd6101c92dfbf0a"),
        "details" : [
                {
                        "attributes" : {
                                "Type" : "Document",
                                "Name" : "Presentation Template, A.1"
                        },
                        "id" : "lLLLL"
                },
                {
                        "attributes" : {
                                "Type" : "Document",
                                "Name" : "Presentation Template, A.2"
                        },
                        "id" : "2LLLL"
                }
        ],
        "__v" : 0
}

所以基本上,没有body.attribute,你需要解析body json数组,并根据您的特定要求获取对象/对象的属性。 希望这有帮助

最新更新