Ich habe ein Modell namens Kategorie und ein anderes namens Produkt. Es gibt eine Beziehung zwischen den beiden Modellen wie:Return-Modell und Beziehungen neu eingefügt
// Category Model
var Category = bookshelf.Model.extend({
tableName : 'categories',
hasTimestamps : true,
products : function() {
return this.hasMany(Product, 'category_id');
}
});
// Product model
var Product = bookshelf.Model.extend({
tableName : 'products',
hasTimestamps : true,
category : function() {
return this.belongsTo(Category, 'category_id');
}
});
ich versuchte, mit 3 Produkten nach dem Beispiel der documentation durch eine Transaktion gegebene Kategorie neben einzufügen. Die Einfügung hat gut funktioniert, aber ich kann nicht alle eingegebenen Daten einschließlich der Produkte zurückgeben (es ist OK für die Kategorie, aber ich bekomme nichts für die neuen Produkte).
, dass meine vollständige Code ist:
bookshelf.transaction(function(t){
var products = req.body.products;
var tableProd = [];
//if (!req.params.idCategory || req.params.idCategory ==0) {
return Category.forge({
name : req.body.name,
code : req.body.code,
tax_percentage : req.body.tax_percentage,
description : req.body.description
})
.save(null, {transacting : t})
.tap(function (category) {
for (var i=0; i < products.length; i++){
tableProd.push({
barcode : products[i].barcode,
cip : products[i].cip,
category_id : category.id,
quantity : products[i].quantity,
retail_price : products[i].retail_price,
description : products[i].description,
name : products[i].name,
DCI_name : products[i].DCI_name,
kind : products[i].kind,
comment : products[i].comment,
status : products[i].status,
critical_threshold : products[i].critical_threshold,
max_threshold : products[i].max_threshold,
date_end : products[i].date_end
});
}
return Promise.map(tableProd, function (info) {
Product.forge(info).save(null, {transacting : t})
})
});
})
.then(function (category) {
var result = category.related('products').pluck('category_id');
console.log('Category' + JSON.stringify(category.toJSON()) + ' resultat: ' + JSON.stringify(result));
res.json({error : false, data : {category : category.toJSON()}})
})
.catch(function (err) {
console.log('Error msg : ' + err.message);
})
Was mit dieser Aussage falsch ist: category.related('products').pluck('category_id')
Wie kann ich alle Daten erhalten eingefügt: die Kategorie, einschließlich der 3 Produkte?
Vielen Dank für Ihre Hilfe
Vielen Dank, Vũ Nguyễn! – Evabila