Javascript 从GET请求将Json主体缓存/保存到db
在了解了如何使用node.js和mean stack的基本知识之后,我想从我自己的项目开始 问题是:Javascript 从GET请求将Json主体缓存/保存到db,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,在了解了如何使用node.js和mean stack的基本知识之后,我想从我自己的项目开始 问题是: 我无法访问请求的API的json主体/属性,我希望将其保存到mongodb并每24小时自动更新一次 我在尝试什么: 是调用2个API。这些响应包含一个json数据集列表,我试图将这些数据集以相同的名称合并到我的mongoose模式中。使用body.param或response.param无效 get请求的响应示例: url1/api/products/all { data: [ { i
我无法访问请求的API的json主体/属性,我希望将其保存到mongodb并每24小时自动更新一次 我在尝试什么:
是调用2个API。这些响应包含一个json数据集列表,我试图将这些数据集以相同的名称合并到我的mongoose模式中。使用body.param或response.param无效 get请求的响应示例:
url1/api/products/all
{
data: [
{
id: 1,
product_name: "Product1",
app: appName,
quantity: 137360,
price: 0.05,
updated_at: "2016-06-04 23:02:03",
median_week: 0.04,
median_month: 0.05,
},
.....
{
id:142640
...
}
}
url2/api/item/all?key=apikey
{
success: true,
num_items: 6713,
products: [
{
product_name: "Product1",
....
icon_url: "//url/economy/image/bla.png",
product_color: "8650AC",
quality_color: "EB4B4B"
},
....
在发现express只路由到本地URL后,我现在使用request。这是我的路线/item.js
var express = require('express');
var qs = require('querystring');
var request = require('request');
var _ = require('underscore');
var router = express.Router();
var Item = require('../models/item');
var options = {
url: 'url2' + ?key + app.config.url2.apikey ,
port: 80,
method: 'GET',
json:true
}
/* GET listing. */
router.get('/', function(req, res) {
request.get('url', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Prints JSON Body
//Iterate through each id of url1 and num_items of url2 and update collection
// Better to make another request method just to call the second api?
for each(item in body.id || body.num_items) {
var newItem = Item({
id: item[i],
product_name: ,
app: 'appName',
quantity: res.body.quantity,
price: res.body.price,
....
icon_url: res.body.,
name_color: res.body.,
quality_color: body.,
created_at: Date.now,
updated_at:
})
newItem.save(function(err) {
if (err) throw err;
console.log('Item created');
})
//return res.json(body);
}
}
res.sendStatus('304');
});
});
这是我的商品型号
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ItemSchema = new Schema({
id: String,
....
created_at: {
type: Date,
default: Date.now
},
updated_at: Date
});
var Item = mongoose.model('Item', ItemSchema);
ItemSchema.pre('save', function(next) {
var currentDate = new Date();
this.updated_at = currentDate;
if (!this.created_at)
this.created_at = currentDate;
next();
});
module.exports = Item;
在写了这个问题之后,我认为这不是最好的方法。有什么最佳做法吗?您有几个问题:
router.get('/', function(req, res) {
// npm install async --save
var async = require('async');
request.get('url', function (error, response, body) {
if (error && response.statusCode !== 200) {
// request could not be completed
return res.send(400, {message: 'Something went wrong'});
} else {
// get the count
var iterator = body.id || body.num_items;
// iterate number of times
async.times(iterator, function(number, next){
// create your new object
var newItem = Item({
id: number,
product_name: ,
app: 'appName',
quantity: req.body.quantity, // <== not res.body
price: req.body.price,
icon_url: req.body.,
name_color: req.body.,
quality_color: body.,
created_at: Date.now
});
// save and call next (which is callback)
newItem.save(next);
}, function(timesErr, timesResult){
// if something failed, return error
// even if 1 item failed to save, it will return error
if(timesErr) {
return res.send(400, {message: 'Something went wrong'});
}
// send the list of saved items;
// or whatever you want
res.status(200).send(timesResult);
});
}
});
});
router.get('/',函数(req,res){
//npm安装异步--保存
var async=require('async');
request.get('url',函数(错误、响应、正文){
if(error&&response.statusCode!==200){
//无法完成请求
return res.send(400,{message:'出了问题'});
}否则{
//数一数
var迭代器=body.id | | body.num_项;
//迭代次数
times(迭代器,函数(number,next){
//创建新对象
var newItem=Item({
身份证号码:,
产品名称:,
应用程序:“appName”,
quantity:req.body.quantity,//您有几个问题:res.body
必须是req.body
,必须异步迭代数组(使用async
package),保存所有数据后发送结果谢谢,我尝试记录所有可以帮助我接收那些body参数的内容,但所有内容都未定义。我在之前使用了req.body,这就是为什么最后使用了im。尽管我将body解析器作为依赖项。下面是一个输出示例{uu v:0,id:“0”,appid:730,更新地址:“2016-06-05T16:18:49.445Z”,id:“575450E9A3AD44E02A0861C”,创建地址:“2016-06-05T16:18:49.408Z”},{id:0,id:“1”,appid:730,更新地址:“2016-06-05T16:18:49.469Z”,id:“575450E9A3AD44E02A008615D”,创建地址:“2016-06-05T16:18:49.430Z”}
如您所见,我无法访问json正文中的数据。这是另一个问题,请打开新的问题,因为这不适合您提供的当前帖子