是的,有很多方法可以解决您的问题。
您可以通过 id ( /db/Shifts/73c81cc9-fa14-4fbe-9839-
10c4121b3fc6
) 或键 ( 73c81cc9-fa14-4fbe-9839-10c4121b3fc6
) 加载对象,load 方法都支持两者。
// resolves both to the same object
db.Shifts.load('/db/Shifts/73c81cc9-fa14-4fbe-9839-10c4121b3fc6')
db.Shifts.load('73c81cc9-fa14-4fbe-9839-10c4121b3fc6')
您可以使用相应的访问器直接从任何对象引用中访问 id 或键:
例如,您有一个对象obj
引用shift
了 Shifts 实例。然后您可以轻松地直接访问引用的 id 或 key。
obj.shift.id == '/db/Shifts/73c81cc9-fa14-4fbe-9839-10c4121b3fc6'
obj.shift.key == '73c81cc9-fa14-4fbe-9839-10c4121b3fc6'
如果要加载该引用,可以load
直接使用引用方法:
obj.shift.load().then(shift => {
shift.property = 'name';
// Note that the obj.shift reference is resolved by the load call
obj.shift === shift;
return shift.save(); //do whatever you want to do with the reference
})
它在我们的对象引用指南中进行了描述。
另一种直接使用引用的 shift 对象加载对象的方法是深度加载您可以使用深度加载通过一次调用来加载对象及其引用:
// The depth: 1 parameter ensures that all directly referenced objects of obj
// will be resolved by the load call
DB.MyClassWithShiftReference.load(id, {depth: 1}).then(obj => {
obj.shift.property = 'name';
})