2017-03-07 75 views
0

我在学习NodeJS并试图在本地建立一个模拟网站。基本上,它是一个使用WOW AuctionHouse API的网站。我从请求中获取数据,将其解析为JSON并尝试将其写入数据库。问题是,如果我把auctions.save放入请求中,它不会将.save函数识别为一个函数(假设它不在该范围之内),并且如果我将它放在问题的外面,则来自异步特性,我的数据库中有一个空条目。这里是代码:获取API数据并使用NodeJS写入MongoDB

var express = require('express'); 
var hbs = require('express-hbs'); 
var mongoose = require('mongoose'); 
//var blizzard = require('blizzard.js').initialize({ apikey: "xxx" }); 

mongoose.connect('mongodb://127.0.0.1/blood_of_sargeras') 

var post = mongoose.model(
    'post', 
    { 
    name: String, 
    type: String, 
    price: String 
    }); 

var app = express(); 

app.engine('hbs', hbs.express4({ 
    partialsDir:__dirname+'/views/partials' 
})); 

app.set('view engine', 'hbs'); 
app.set('views', __dirname + '/views'); 

app.use('/static', express.static('static')); 

var request = require("request"); 
var url = "http://auction-api-eu.worldofwarcraft.com/auction-data/xxx-xxx/auctions.json" 
var auctions = new post; 

request({ 
    url: url, 
    json: true 
}, function (error, response, body) { 

    if (!error && response.statusCode === 200) { 
     auctions = JSON.stringify(body); 
     post.save(function (err) { 
      if(err) 
      console.log("Error with database!"); 
      else 
      console.log("Data written to DB!"); 

     }) 
    } 
}); 

app.get('/', function (req, res){ 

    post.find({}, function (err, o){ 
    if (err) 
     console.log('Database error!'); 
    else{ 
     res.render('index', o); 
    } 
    }); 
}); 

app.listen(80, function(){ 
    console.log('Listening on port 80') 
}); 

这是甚至正确的方法吗?有没有更好的办法?我一直坚持这一段时间,它变得非常令人沮丧。其他的事情我想是:要复制,因此等待整个事情的要求外

  • 超时功能,但不工作我得到同样的问题,因为里面尝试它 ,它说.save不是函数
  • 尝试调用在outtermost作用域上定义的函数,而在请求内但这也告诉我.save不是函数。
+0

尝试这种情况: VAR postSchema = mongoose.Schema({名称:字符串,MTYPE:字符串,价格:串}); var post = mongoose.model('Post',postSchema); var newPost = new Kitten(); newPost.name = newPost.mType = //不使用它被保留型 newPost.price = newPost.save(功能(ERR,mPost){ 如果(ERR)console.error(ERR); console.log(mPost); }); –

+0

林不知道这是什么suposed做的,但我得到确切同样的错误: newPost.price = newPost.save(函数(ERR,mPost){ ^ 类型错误:newPost.save不是一个函数 另外,据我计算出它与猫鼬的范围(我可以使用它的范围)的问题 – Dragan

回答

1

我想你可能需要将你的请求包装在一个事件监听器中。这将确保请求在服务器完成启动后运行,而不是之前。这是一个简单的例子。在请求以后

var postSchema = mongoose.Schema({name: String,mType: String,price: String}); 
var post = mongoose.model('Post',postSchema); 

var newPost = new Kitten(); 

然后::

app.on('listening', function() { 
    // Run your request to the WoW auction house once the express server has started up 
    request({ 
     url: url, 
     json: true 
    }, function (error, response, body) { 

     if (!error && response.statusCode === 200) { 
      auctions = JSON.stringify(body); 
      post.save(function (err) { 
       if(err) 
        console.log("Error with database!"); 
       else 
        console.log("Data written to DB!"); 

      }) 
     } 
    }); 
}); 
+0

与整个时间相同的错误,auctions.save不是一个函数(它工作,如果我尝试它之外的任何函数) 编辑:对不起,实际上没有任何反应,没有代码得到执行(没有控制台日志,没有任何东西) – Dragan

+0

我已经编辑了你的代码,这应该工作,你需要决定如何保存数据到我可以看到拍卖的数据库.json文件相当大,因此您可能需要循环浏览它。 我在这里创建了一个要点 https://gist.github.com/Fried-Chicken/7955f671b8ac9da08328752335d13b3c –

+0

这很棒,它的工作原理,但另一个问题弹出。 auctions.insert插入一个无用的值,而不是我在body变量中获得的值。如果我尝试body.save,它说它不是一个函数,并且我尝试过使用json.parse和json.stringify以及它的同样的问题,我认为它会停止将拍卖视为“发布”对象。 – Dragan

0

尝试此

newPost.name = auctions.name; 
newPost.mType = auctions.type; //do not use type it is reserved 
newPost.price = auctions.price 

newPost.save(function(err, mPost) { 
    if (err) console.error(err); 
    console.log(mPost); 
});