2016-10-05 11 views
2

私は何時までもこの問題を解決するために何時間も持っています。私は助けにならない初心者です。以下は、コマンドライン(ウィンドウ)からシードを実行した後のJSON出力です。以下はMongoDBデータベースを「null」を取得せずにシードする方法

{ 
 
    "_id":"57f4fc5adf7e1109b08660d4", 
 
    "companyName":"Flat Iron", 
 
    "contactName":"Phil", 
 
    "userName":"Hook", 
 
    "email":"[email protected]", 
 
    "phone":"12345678910", 
 
    "image":"url6", 
 
    "meals":[ 
 
     { 
 
     "_id":"57f4fc5adf7e1109b08660d1", 
 
     "name":"Burger and chips", 
 
     "upvotes":5, 
 
     "downvotes":20, 
 
     "price":16, 
 
     "official":true, 
 
     "__v":0, 
 
     "images":[ 
 
      "google.com/url3" 
 
     ], 
 
     "ingredients":[ 
 
      "egg", 
 
      "water", 
 
      "GF dough", 
 
      "beef" 
 
     ], 
 
     "suitable_for":[ 
 
      "fodmap" 
 
     ], 
 
     "add_for_taste":[ 
 
      "asoefida", 
 
      "orange", 
 
      "pineapple" 
 
     ], 
 
     "remove_for_safe":[ 
 
      "garlic", 
 
      "wheat" 
 
     ], 
 
     "favourited_by":[ 
 
      null 
 
     ] 
 
     } 
 
    ], 
 
    "locations":[ 
 
     "NW16BG", 
 
     "EN41RT" 
 
    ], 
 
    "caters_for":[ 
 
     "scd", 
 
     "paleo", 
 
     "fodmap" 
 
    ] 
 
} 
 
]

私の種子ファイルです。上から、 'favourited_by'はnullを返しますが、正しい数のオブジェクト(または 'null')を取得することがわかります。

var mongoose = require("mongoose"); 
 

 
var databaseURL = 'mongodb://localhost:27017/tda'; 
 
mongoose.connect(databaseURL); 
 

 
// var Project = require("../models/project"); 
 
var User = require("../models/user"); 
 
var Vendor = require("../models/vendor"); 
 
var Meal = require("../models/meal"); 
 

 
// This will clear what ever we have in the database :) 
 
User.collection.drop(); 
 
Vendor.collection.drop(); 
 
Meal.collection.drop(); 
 

 
var meal1 = new Meal({ 
 
    name: "Nice Meal", 
 
    upvotes: 15, 
 
    downvotes: 3,  
 
    vendor: "Flat Iron", 
 
    favourited_by: [user1, user2], 
 
    price: 12, 
 
    remove_for_safe: ["onions", "cheese"], 
 
    add_for_taste: ["carrots"], 
 
    suitable_for: ["paleo", "fodmap", "SCD"], 
 
    ingredients: ["egg", "water", "cress"], 
 
    images: ["google.com/url"], 
 
    official: true 
 
}) 
 

 

 

 

 
var meal2 = new Meal({ 
 
    name: "Good meal", 
 
    upvotes: 35, 
 
    downvotes: 23,  
 
    vendor: "McDonalds", 
 
    favourited_by: [user1, user2, user3], 
 
    price: 6, 
 
    remove_for_safe: ["onions", "gherkins"], 
 
    add_for_taste: ["carrots", "peas", "water"], 
 
    suitable_for: ["paleo", "fodmap"], 
 
    ingredients: ["egg", "water", "GF Bread", "burger"], 
 
    images: ["google.com/url2"], 
 
    official: false 
 
}) 
 

 

 

 

 

 
var meal3 = new Meal({ 
 
    name: "Burger and chips", 
 
    upvotes: 5, 
 
    downvotes: 20,  
 
    vendor: "Reds True BBQ", 
 
    favourited_by: [user3], 
 
    price: 16, 
 
    remove_for_safe: ["garlic", "wheat"], 
 
    add_for_taste: ["asoefida", "orange", "pineapple"], 
 
    suitable_for: ["fodmap"], 
 
    ingredients: ["egg", "water", "GF dough", "beef"], 
 
    images: ["google.com/url3"], 
 
    official: true 
 
}) 
 

 

 

 

 

 
var vendor1 = new Vendor({ 
 
    companyName: "Reds", 
 
    contactName: "Butch", 
 
    userName: "Hook", 
 
    email: "[email protected]", 
 
    phone: "12345678910", 
 
    image: "url4", 
 
    caters_for: ["scd", "paleo", "fodmap"], 
 
    locations: ["N213BQ", "EN41RT"], 
 
    meals: [meal1, meal2] 
 
}) 
 

 

 
var vendor2 = new Vendor({ 
 
    companyName: "McDonalds", 
 
    contactName: "Tim Applebee", 
 
    userName: "TimA", 
 
    email: "[email protected]", 
 
    phone: "12345678910", 
 
    image: "url5", 
 
    caters_for: ["scd", "paleo", "fodmap"], 
 
    locations: ["N15TG", "EC41RT"], 
 
    meals: [meal2] 
 
}) 
 

 

 
var vendor3 = new Vendor({ 
 
    companyName: "Flat Iron", 
 
    contactName: "Phil", 
 
    userName: "Hook", 
 
    email: "[email protected]", 
 
    phone: "12345678910", 
 
    image: "url6", 
 
    caters_for: ["scd", "paleo", "fodmap"], 
 
    locations: ["NW16BG", "EN41RT"], 
 
    meals: [meal3] 
 
}) 
 

 

 
var user1 = new User({ 
 
    firstName: "Mick", 
 
    lastName: "Fry", 
 
    userName: "Mike", 
 
    email: "[email protected]", 
 
    diet: "fodmap", 
 
    location: "London", 
 
    profile_photo: "imageurl", 
 
    saved_meals: [meal1, meal2], 
 
    saved_vendors: [vendor1] 
 
}) 
 

 

 
var user2 = new User({ 
 
    firstName: "Tom", 
 
    lastName: "Scott", 
 
    userName: "tascott", 
 
    email: "[email protected]", 
 
    diet: "fodmap", 
 
    location: "Leeds", 
 
    profile_photo: "imageurl2", 
 
    saved_meals: [meal1, meal3], 
 
    saved_vendors: [vendor1] 
 
}) 
 

 

 
var user3 = new User({ 
 
    firstName: "Phil", 
 
    lastName: "Croy", 
 
    userName: "Croa", 
 
    email: "[email protected]", 
 
    diet: "SCD", 
 
    location: "London", 
 
    profile_photo: "imageurl3", 
 
    saved_meals: [meal3], 
 
    saved_vendors: [vendor1, vendor2, vendor3] 
 
}) 
 

 

 

 

 
// Save the users 
 
user1.save(function(err, user) { 
 
if (err) return console.log(err); 
 
console.log("User saved! ", user); 
 
}) 
 
user2.save(function(err, user) { 
 
if (err) return console.log(err); 
 
console.log("User saved! ", user); 
 
}) 
 
user3.save(function(err, user) { 
 
if (err) return console.log(err); 
 
console.log("User saved! ", user); 
 
}) 
 

 

 
// Save the Vendors 
 
vendor1.save(function(err, vendor) { 
 
if (err) return console.log(err); 
 
console.log("Vendor saved! ", vendor); 
 
}) 
 
vendor2.save(function(err, vendor) { 
 
if (err) return console.log(err); 
 
console.log("Vendor saved! ", vendor); 
 
}) 
 
vendor3.save(function(err, vendor) { 
 
if (err) return console.log(err); 
 
console.log("Vendor saved! ", vendor); 
 
}) 
 

 

 

 

 

 
meal1.save(function(err, meal) { 
 
if (err) return console.log(err); 
 
console.log("Meal saved! ", meal); 
 
}) 
 

 

 
meal2.save(function(err, meal) { 
 
if (err) return console.log(err); 
 
console.log("Meal saved! ", meal); 
 
}) 
 

 

 

 
meal3.save(function(err, meal) { 
 
if (err) return console.log(err); 
 
console.log("Meal saved! ", meal); 
 
})

私は「favourited_by」は種子のファイルで最後に定義されている「ユーザー」を埋め込まれているので、それがある印象でいます。したがって、シードを実行すると、ユーザーにデータを入力しようとしますが、データはまだ表示されません。

誰かがこれを回避する方法をお手伝いできますか?

答えて

1

はい、favourited_byを設定すると、ユーザーは未定義です。なぜなら、ユーザーはnullとして保存されるからです。ユーザーを設定した後に食事を更新しようとする可能性があります。

meal1.favourited_by = [user1._id, user2._id] 

食事の初期設定からこれを削除することもできます。

+0

これを前に追加しました//ユーザを一番下に保存します。最大コールスタックサイズ:私はノードのdb /シーズを実行したときに meal1.favourited_by = [ユーザー1、ユーザー2] meal2.favourited_by = [ユーザー1、ユーザー2、ユーザー3] meal3.favourited_by = [USER3] は、しかし、今私は、」例外RangeErrorを取得します超過した " – TaScott

+1

ああ、私はそれが来るのを見なかった。私は、あなたが "meal1.favourited_by = [user1]"のような循環的な割り当てを持っていて、ユーザが "user1.meals = [meal1]"のようなものを持っているので、これが起こったと思います。両方の方法で保管する必要がある場合は、文書のIDのみを保管してください。回答を更新して – andresk

+0

に更新します。少なくとも、IDでデータを入力してnullにならないようにしています。ありがとう! – TaScott

関連する問題