【发布时间】:2016-02-08 19:55:57
【问题描述】:
我正在尝试解析来自 url 的一些 json 数据并将其保存到我的 mongoDB 模型中。但是我似乎无法从body 正确解析 JSON。我怎样才能做到这一点?
代码
router.get('/news', function(req, res){
request({
method: "GET",
url: "URL",
json: true
}, function(err, response, body) {
console.log(err);
res.json(body);
var info = JSON.parse(body);
console.log(info.articles);
})
});
来自 api 的代码片段
{
"articles": [
{
"title": "this is the title",
"created": "12-09-2015",
"author": "John Doe",
"image": "http://url.com/test.jpg",
"body": "this is the body"
},
{
"title": "this is the title",
"created": "12-09-2015",
"author": "John Doe",
"image": "http://url.com/test.jpg",
"body": "this is the body"
}
]
}
新闻模型
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var newsSchema = new Schema({
title: String,
created: String,
author: String,
image: String,
bodyfull: String
});
module.exports = mongoose.model('news', newsSchema);
【问题讨论】:
标签: json node.js mongodb express mongoose