创建一个存储许多不同类型对象的应用程序,它们的唯一属性存储在不同的表中,并且与具有由所有对象共享的一般属性的表关联,并且该表必须与存储用户输入的指定位置的表格。
我已经创建了表及其关联,代码示例如下: 这是一般属性表:
var AssetInfo = sequelize.define('AssetInfo', {
invoiceNumber: {type:Sequelize.STRING, allowNull:false},
purchaseOrder: {type:Sequelize.STRING, allowNull:false},
physicalCondition: {type:Sequelize.STRING, allowNull:false},
modelName: {type:Sequelize.STRING, allowNull:false},
supplier: {type:Sequelize.STRING, allowNull:false},
manufacturer: {type:Sequelize.STRING, allowNull:false},
status: {type:Sequelize.STRING, allowNull:false},
purchaseCost: {type:Sequelize.DOUBLE, allowNull:false},
acquiredDate: {type:Sequelize.DATE, allowNull:false},
lostOrStolen: {type:Sequelize.BOOLEAN, allowNull:false, defaultValue:false},
lOSDate: {type:Sequelize.DATE, allowNull:true},
usefulLife: {type:Sequelize.INTEGER, allowNull:true},
warrantyExpiry: {type:Sequelize.DATE, allowNull:false},
depreciateRate: {type:Sequelize.DOUBLE, allowNull:false},
scrapped: {type:Sequelize.BOOLEAN, allowNull:false, defaultValue:false},
dateScrapped: {type:Sequelize.DATE, allowNull:true},
scrappedTo: {type:Sequelize.STRING, allowNull:true},
sold: {type:Sequelize.BOOLEAN, allowNull:false, defaultValue:false},
dateSold: {type:Sequelize.DATE, allowNull:true},
sellPrice: {type:Sequelize.DOUBLE, allowNull:true},
priceLoss: {type:Sequelize.DOUBLE, allowNull:true},
currentCost: {type:Sequelize.DOUBLE, allowNull:false},
netRealisableValue: {type:Sequelize.DOUBLE, allowNull:false},
fairValue: {type:Sequelize.DOUBLE, allowNull:false},
picture: {type:Sequelize.STRING, allowNull:true},
description: {type:Sequelize.STRING, allowNull:true}},
{freezeTableName:true}
);
这是对象表和位置表之一:
var Computer = sequelize.define('Computer', {
serial: {type:Sequelize.STRING, allowNull:false},
user: {type:Sequelize.STRING, allowNull:false}},
{freezeTableName:true
});
var CompanyLocation = sequelize.define('CompanyLocation', {
companyName: {type:Sequelize.STRING, allowNull:false},
site: {type:Sequelize.STRING, allowNull:false}},
{freezeTableName:true}
);
CompanyLocation.hasMany(AssetInfo);
Computer.hasMany(AssetInfo);
AssetInfo.belongsTo(Computer);
我能够通过以下方式创建对象和常规信息表的关联:
function addComputer(inv, pur, phys, mod, sup, manu, stat, purC, acD, useLife,warr, dep, cur, netRe, fair, pic, desc, num, use){
sequelize.Promise.all([
Computer.create({serial : num,user : use}),
AssetInfo.create({invoiceNumber : inv, purchaseOrder: pur,
physicalCondition : phys, modelName : mod, supplier : sup ,
manufacturer : manu, status : stat, purchaseCost : purC,
acquiredDate : acD, usefulLife : useLife, warrantyExpiry : warr,
depreciateRate : dep, currentCost : cur, netRealisableValue : netRe,
fairValue: fair, picture : pic, description : desc})
]).spread(function (comp, assIn) {
return comp.addAssetInfo(assIn);
});
}
并尝试过按名称查找预制位置并尝试将其与新创建的对象及其常规信息表关联起来非常不成功的事情。
我无法在文档或其他论坛中找到任何可以使代码生效的内容,它只是简单地无法关联它们。任何帮助将不胜感激。
我尝试将预设位置导入到原始创建功能中,如:
function addComputer(inv, pur, phys, mod, sup, manu, stat, purC, acD, useLife,warr, dep, cur, netRe, fair, pic, desc, num, use){
sequelize.Promise.all([
Computer.create({serial : num,user : use}),
AssetInfo.create({invoiceNumber : inv, purchaseOrder: pur,
physicalCondition : phys, modelName : mod, supplier : sup ,
manufacturer : manu, status : stat, purchaseCost : purC,
acquiredDate : acD, usefulLife : useLife, warrantyExpiry : warr,
depreciateRate : dep, currentCost : cur, netRealisableValue : netRe,
fairValue: fair, picture : pic, description : desc})
]).spread(function (comp, assIn) {
return comp.addAssetInfo(assIn);
}).**Then(function(assIn)
var locate = Location.findById(#);
assIn.addLocation(locate));**
}
seoond line被标记为无法访问的文本,并且不会运行
然后我创建了一个单独的函数来在创建它们之后更新它们:
function addLocate(){
var locate = Location.findById(#);
var assIn = AssetInfo.findById(#);
assIn.addLocation(locate);
}
给出了assIn.addLocation不是函数的错误(另请注意我重写了这段代码没有直接复制,删除原文,因为它没有用,所以所有语法都不准确,例如#是一个数字, addLocation是addCompanyLocation)
答案 0 :(得分:0)
create
返回一个承诺,就像function addComputer(inv, pur, phys, mod, sup, manu, stat, purC, acD, useLife, warr, dep, cur, netRe, fair, pic, desc, num, use) {
sequelize.Promise.all([
Computer.create({
serial: num,
user: use
}),
AssetInfo.create({
invoiceNumber: inv,
purchaseOrder: pur,
physicalCondition: phys,
modelName: mod,
supplier: sup,
manufacturer: manu,
status: stat,
purchaseCost: purC,
acquiredDate: acD,
usefulLife: useLife,
warrantyExpiry: warr,
depreciateRate: dep,
currentCost: cur,
netRealisableValue: netRe,
fairValue: fair,
picture: pic,
description: desc
})
]).spread(function(comp, assIn) {
return comp.addAssetInfo(assIn);
}).then(function(assIn) {
return Location.findById(#).then(function (locate) {
return assIn.addLocation(locate));
});
});
}
一样 - 您必须等待查询完成
@".\Web\index.htm"