如何通过Javascript

时间:2017-08-28 03:41:41

标签: javascript skygear

我正在尝试使用skygear-SDK-JS https://github.com/SkygearIO/skygear-SDK-JS在两种记录类型之间创建一对一的关系,并按照指南https://docs.skygear.io/guides/cloud-db/data-types/js/使用Reference保存记录。如指南中所述,Skygear服务器将为我创建外键约束。

但是,我找不到如何使用skygear.query调用Reference API。

例如,从指南中复制以下代码。

const note1 = new Note({
  heading: 'Specification',
  content: 'This is first section',
});
const note2 = new Note({
  heading: 'Specification page 2',
  content: 'This is second section',
});
note1.nextPage = new skygear.Reference(note2);
skygear.publicDB.save([note2, note1]);

现在我想找到Note nextPage heading Specification page 2 //Assuming 32 bit integers int is_diff_positive(int num) { ((num & 0x80000000) >> 31) ^ 1; // if diff positive ret 1 else 0 } int sign(int x) { return ((num & 0x80000000) >> 31); } int flip(int x) { return x ^ 1; } int max(int a, int b) { int diff = a - b; int is_pos_a = sign(a); int is_pos_b = sign(b); int is_diff_positive = diff_positive(diff); int is_diff_neg = flip(is_diff_positive); // diff (a - b) will overflow / underflow if signs are opposite // ex: a = INT_MAX , b = -3 then a - b => INT_MAX - (-3) => INT_MAX + 3 int can_overflow = is_pos_a ^ is_pos_b; int cannot_overflow = flip(can_overflow); int res = (cannot_overflow * ( (a * is_diff_positive) + (b * is_diff_negative)) + (can_overflow * ( (a * is_pos_a) + (b * is_pos_b))); return res; } ,我应该如何构建查询对象?

1 个答案:

答案 0 :(得分:0)

要通过引用查询Skygear记录,只需构建一个在引用列上具有equalTo谓词的查询对象,如下所示:

let query = new skygear.Query(Note);
query.equalTo('nextPage', new skygear.Reference({
  id: 'note/uuid-of-note2'
}));
skygear.publicDB.query(query).then((notes) => {
  console.log(notes[0]); // You should got note1
});

在您的用例中,以下代码应该有效。

let query = new skygear.Query(Note);
query.equalTo('heading', 'Specification page 2');
skygear.publicDB.query(query).then((notes) => {
  let refQuery = new skygear.Query(Note);
  refQuery.equalTo('nextPage', new skygear.Reference(notes[0]));
  skygear.publicDB.query(refQuery).then((result) => {
    console.log(result);
  });
});