SHARE
TWEET

Untitled

a guest Sep 16th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // import mongoose Schema
  2. const Restaurant = require('../restaurant')
  3.  
  4. // import restaurant.json
  5. const restaurantsList = require('./restaurant.json')
  6.  
  7. // import mongoose and connect to mongoDB
  8. const mongoose = require('mongoose')
  9. mongoose.connect('mongodb://localhost/restaurant', { useNewUrlParser: true })
  10. const db = mongoose.connection
  11.  
  12. // actions if connect error
  13. db.on('err', (err) => {
  14.   if (err) return console.error(err)
  15. })
  16.  
  17. // actions if connect success
  18. db.once('open', (err) => {
  19.   if (err) return console.error(err)
  20.   console.log('connect to mongoDB successifully !')
  21. })
  22.  
  23. // 迭代每一筆餐廳資料,以 Restaurant(mongoose Schema 之實例來儲存至 mongoDB)
  24. restaurantsList.results.forEach(element => {
  25.   Restaurant.create({
  26.     name: element.name,
  27.     name_en: element.name_en,
  28.     category: element.category,
  29.     image: element.image,
  30.     location: element.location,
  31.     phone: element.phone,
  32.     google_map: element.google_map,
  33.     rating: element.rating,
  34.     description: element.description
  35.   }, (err, restaurants) => {
  36.     if (err) return console.error(err)
  37.  
  38.   })
  39. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top