2015-02-10 61 views
0

我在最後幾個小時撞到了我的頭,我無法弄清楚我的問題的解決方案。在我的風帆模型中,我有2個一對多的關聯。 'A'模型可以有許多'B','B'模型可以有許多'C'。在我的控制器中,當我執行a.find().populate('b') (...)時,它會返回A模型的整個集合,但只填充一個B的A模型的每個條目,這與我的數據庫(mysql)中的當前數據集不匹配。並且不填充B模型中的C條目。換句話說,我試圖實現類似嵌套人口。
控制器代碼有問題,對吧?我該如何做這項工作? 在此先感謝!風帆/水線填充不能按預期工作

編輯:

Company.js

module.exports = { 

identity: 'company', 

attributes: { 

    name: { 
     type: 'string', 
     required: true 
    }, 

    address: { 
     type: 'string', 
     required: true 
    }, 

    zip_code: { 
     type: 'string', 
     required: true 
    }, 

    city: { 
     type: 'string', 
     required: true 
    }, 

    nif: { 
     type: 'integer', 
     required: true, 
     minLength: 9 
    }, 

    country: { 
     type: 'string', 
     required: true 
    }, 

    phone_number: { 
     type: 'string', 
     required: true 
    }, 

    email: { 
     type: 'email', 
     required: true 
    }, 

    facilities: { 
     collection: 'facility', 
     references: 'facility', 
     on: 'id', 
     via: 'company' 
    } 
    } 
}; 

Facility.js

module.exports = { 

identity: 'facility', 

attributes: { 

    company: { 
     columnName: 'id_company', 
     model: 'company' 
    }, 

    warehouses: { 
     collection: 'warehouse', 
     references: 'warehouse', 
     on: 'id', 
     via: 'facility' 
    }, 

    name: { 
     type: 'string', 
     required: true 
    }, 

    address: { 
     type: 'string', 
     required: true 
    }, 

    zip_code: { 
     type: 'string', 
     required: true 
    }, 

    city: { 
     type: 'string', 
     required: true 
    }, 

    country: { 
     type: 'string', 
     required: true 
    }, 

    phone_number: { 
     type: 'string', 
    }, 

    email: { 
     type: 'email', 
    }, 

    longitude: { 
     type: 'float', 
    }, 

    latitude: { 
     type: 'float' 
    } 

    } 
}; 

Warehouse.js

module.exports = { 

identity: 'warehouse', 

attributes: { 

    facility: { 
     columnName: 'id_facility', 
     model: 'facility', 
    }, 

    name: { 
     type: 'string', 
     required: true 
    }, 

    longitude: { 
     type: 'float', 
    }, 

    latitude: { 
     type: 'float' 
    } 

    } 
}; 

MainControl LER的相關代碼:

companies: function(req, res) { 
    company.find().populate('facilities').exec(function(err, comp){ 
     var error = ''; 
     if(err){ 
      error = 'Unable to retrieve the requested information. Try again later and, if the problem persists, contact the platform administrator.'; 
     } else if(!comp[0]) { 
      error = 'There\'s no company data inserted.'; 
     } 
     // (...) 
    }); 

}, 
+0

你能分享你的模型和控制器代碼? – irobert91 2015-02-10 11:18:44

+0

@RobertIgnat謝謝你的回覆。我爲這個問題添加了一些相關的代碼。 – 2015-02-10 12:10:17

+0

首先,Waterline目前不支持嵌套人口,所以'Company.find()。populate('facilities')'只會填充每個公司的設施,而不是每個設施的倉庫。 其次,你有沒有嘗試刪除模型中的'references'和'on'?如果是的,你會得到相同的行爲,你能舉出一個反應的例子嗎? – irobert91 2015-02-10 16:27:38

回答

1

您應該刪除從模型的referenceson

關於嵌套人口,就像我在評論中所說的,Waterline目前不支持它。您可以檢查Waterline2,正如他們所說,它提供了嵌套填充的可能性,但不建議用於生產。
否則你可以檢查了這一點:Sails.js populate nested associations

+0

在這一刻,我無法切換到waterline2,因爲我的應用程序有大量代碼,並且很快就會切換到生產。但我會檢查新的水線!謝謝! :) – 2015-02-10 18:24:00