2017-04-03 76 views
-1

我想保存这个json数组,我的输出返回到数据库。不过,我真的不知道如何使用nodejs将其转换为字符串。当我尝试插入数据库。我得到所有行的'未定义'值。 什么是替代方案?我想要一个json文件文件作为输出以及想要在MySQL数据库中插入值。请帮忙。感谢当我尝试插入数据库。我得到'undefined'值的所有行

var express = require('express'); 
var fs = require('fs'); 
var mysql = require('mysql'); 
var request = require('request'); 
var cheerio = require('cheerio'); 
var bodyParser = require('body-parser'); 
var app  = express(); 

var output; 
app.use(bodyParser.json()) 

app.get('/scrape', function(req, res){ 
    url = 'https://raitalumni.dypatil.edu/events/?tag=live'; 

    request(url, function(error, response, html){ 
     if(!error){ 
      var $ = cheerio.load(html); 
      var json = { 
          title : [], 
          date : [], 
          month : [], 
          venue : [], 
          link : [] 
         }; 
      output = { 
          events : [] 
         }; 

      $('p.event_name').each(function(){ 
       json.title.push($(this).text()); 
      }); 




      $('p.calendar_date').each(function(){ 
       json.date.push($(this).text()); 
      }); 

      $('span.calendar_month').each(function(){ 
       json.month.push($(this).text()); 
      }); 


      //var fulldate = $('p.calendar_date').concat($('p.calendar_day')).text(); 
      //console.log('all records: ' + fulldate); 

      $('p.event_venue').each(function(){ 
       json.venue.push($(this).text()); 
      }); 

     // var title = $('p.event_name').each(function(){$(this).text()}); 



      for(var i=0; i<json.title.length; i++){ 
       output.events[i] = { 
        title : json.title[i], 
        date : json.date[i], 
        month : json.month[i], 
        venue : json.venue[i], 
        link : url 
       } 
      } 

     var connection = mysql.createConnection({ 
     host: '127.0.0.1', 
     port: 3306, 
     user: 'root', 
     password: '', 
     database: 'raithub' 
    }); 


connection.connect(function(error){ 
    if(!!error){ 
     console.log('Error'); 
    }else{ 
     console.log('Connected to the database!'); 
    } 
}); 



     var scrape = JSON.stringify(output, null, 4); 
     //var obj = JSON.parse(output); 
     //console.log(obj); 

      var query = connection.query("INSERT INTO scrapped_data (title,date,month,venue,link) VALUES ('" + output.title + "', '" + output.date + "', '" + output.month + "', '" + output.venue + "', '" + output.link + "');", scrape, function(err, result) { 
    if(err) throw err; 
    console.log('data inserted'); 
}); 


      fs.writeFile('output4.json', JSON.stringify(output, null, 4), function(err){ 
       console.log('File successfully written! - Check your project directory for the output.json file'); 
      }) 

      res.send('Check your console!')   
     } 
     else { 
      console.log("Network Error, please try again later") 
     } 
    }) 
}) 
app.listen('8000') 
console.log('Server running on port 8081'); 
exports = module.exports = app;  

output.json:

{ 
    "events": [ 
     { 
      "title": "Convocation 2017", 
      "date": "25", 
      "month": "Feb", 
      "venue": "Venue: Ramrao Adik Institute of Technology, Nerul", 
      "link": "https://raitalumni.dypatil.edu/events/?tag=live" 
     }, 
     { 
      "title": "RAIT Alumni Meet 2017", 
      "date": "18", 
      "month": "Feb", 
      "venue": "Venue: Ramrao Adik Institute of Technology, Nerul", 
      "link": "https://raitalumni.dypatil.edu/events/?tag=live" 
     } 
    ] 
} 

回答

0

我想我找到了你的问题。 在这些行中,您将创建一组属于输出对象的事件数组。

for (var i = 0; i < json.title.length; i++) { 
       output.events[i] = { 
        title: json.title[i], 
        date: json.date[i], 
        month: json.month[i], 
        venue: json.venue[i], 
        link: url 
       } 
      } 


所以在你的数据库中插入你应该改变这样的output.titleoutput.events[i].title等:

for (let i = 0; i < json.title.length; ++i) 
var query = connection.query("INSERT INTO scrapped_data (title,date,month,venue,link) VALUES ('" + output.events[i].title + "', '" + output.events[i].date + "', '" + output.events[i].month + "', '" + output.events[i].venue + "', '" + output.events[i].link + "');", scrape, function(err, result) { 
    if (err) throw err; 
    console.log('data inserted'); 
}); 

我还要改变json.title.length第二foroutput.events.length 。 您必须逐行插入这些事件。这就是为什么你没有定义,因为output没有title属性。

相关问题