2016-03-09 19 views
0

我是新來的節點js和mongodb。只是測試一些想法,所以我試圖創建一個網絡刮板,在mongodb中添加數據到數據庫。我有一個連接到mongodb的腳本,並通過節點js腳本動態地將數據添加到數據庫。我在控制檯中運行這樣的腳本,如下所示:'node scrapeData.js'腳本運行時沒有任何錯誤,但是當我啓動mongo shell並運行db.posts.find()時,我得到了0條記錄。我知道我的腳本正在成功地抓取數據,因爲它正在控制檯中記錄數據的數組。不知道我哪裏錯了。這裏是我的腳本:無法通過節點js將文檔數組插入到mongodb中的數據庫中

var MongoClient = require('mongodb').MongoClient; 


//requiring the module so we can access them later on 
var request = require("request"); 
var cheerio = require("cheerio"); 

MongoClient.connect('mongodb://127.0.0.1:27017/mydb', function (err, db) { 
    if (err) { 
     throw err; 
    } 
    else { 
     console.log("successfully connected to the database"); 

     //define url to download 
     var url = "http://www.nyxcosmetics.ca/en_CA/highlight-contour"; 

     var prodList = []; 
     var priceList = []; 
     var products = []; 

     request(url, function(error, response, body) { 
      if(!error) { 


       //load page into cheerio 
       var $ = cheerio.load(body); 

       $(".product_tile_wrapper").each(function(i, elem) { 
        prodList[i] = $(this).find($(".product_name")).attr("title"); 
        priceList[i] = $(this).find($(".product_price")).attr("data-pricevalue"); 
       }); 
       prodList.join(', '); 

       for(var i = 0; i < prodList.length; i++) { 
        var prod = { 
         name: prodList[i], 
         price: priceList[i] 
        }; 
        products.push(prod); 
       } 

       console.log(products); //print the array of scraped data 

       //insert the prods into the database 
       //in the 'posts' collection 
       var collection = db.collection('posts'); 
       collection.insert(products); 
       console.log("products inserted into posts collection"); 

       //Locate all the entries using find 
       collection.find().toArray(function(err, results) { 
        console.log(results); 
       }); 

      } else { 
       console.log("We've encountered an error!") 
      } 
     }); 
    } 

    db.close(); 
}); 

回答

2

只是一些提示:

  • 您可以檢查是否使用的是MongoDB的驅動程序的NodeJS版本? 1.x2.x在2.x的驅動程序版本截然不同
  • db.collection.insert()實際上棄用,取而代之的更詳細的.insertOne().insertMany()
  • 你可以調試你的插入發生了什麼事,如果你提供一個寫回調,例如

collection.insert(products, function(error,result) { console.log(error); console.log(result); })

+0

感謝您的答覆!原來版本不是問題。我使用上面的回調代碼進行了調試,發現錯誤信息如'mongoError:Topology was destroyed'。這與db.close()語句有關。顯然,我不應該在節點js腳本中擁有它。我真的習慣於sql,因此關閉與db的連接看起來很正常,但這在MongoDB中不是很好的做法。這篇文章也幫助了我:http://stackoverflow.com/questions/30425739/mongolab-nodejs-topology-destroyed – rads89

相關問題