beforeSave 的时候获取不到对象的属性值 1 2 3 4 5 6 7 AV .Cloud .beforeSave ('GiftUserMap' , async function (request ) { async function reduceDiamond ( ) { let gift = request.object .get ('gift' ); let giftName = gift.get ('name' ) } }
创建对象 1 2 3 4 5 6 7 8 9 10 const reward = new AV .Object ('Reward' );reward.set ('name' , 'Q 币 5 个' ); reward.save ().then ((reward ) => { console .log ('Save Success' ) })
获取对象 根据键值对,返回数组
1 2 3 4 5 6 7 8 9 new AV .Query ('Reward' ) .equalTo ('objectId' , options.id ) .find () .then (result => { let simpleResult = jsonify (result)[0 ]; console .log (simpleResult); this .setData ({reward : simpleResult}); }) .catch (console .error );
根据 ID,返回单个值
1 2 3 4 5 6 const query = new AV .Query ('Reward' );query.get (options.id ).then ((result ) => { let simpleResult = jsonify (result); console .log (simpleResult) this .setData ({reward : simpleResult}); });
一些常用的查询 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 query.equalTo ('firstName' , 'Jack' ); query.notEqualTo ('firstName' , 'Jack' ); query.doesNotExist ('doubanId); // 查找包含 ' doubanId' 字段的对象,doubanID 不为 undefined query.exists(' doubanId);query.lessThan ('age' , 18 ); query.lessThanOrEqualTo ('age' , 18 ); query.greaterThan ('age' , 18 ); query.greaterThanOrEqualTo ('age' , 18 ); query.limit (10 ); query.first () query.skip (20 ); const query = new AV .Query ('Todo' );query.limit (10 ); query.skip (20 ); const priorityQuery = new AV .Query ('Todo' );priorityQuery.greaterThanOrEqualTo ('priority' , 3 ); const isCompleteQuery = new AV .Query ('Todo' );isCompleteQuery.equalTo ('isComplete' , true ); const query = AV .Query .or (priorityQuery, isCompleteQuery);const studentQuery = new AV .Query ('Student' );const countryQuery = new AV .Query ('Country' );countryQuery.equalTo ('language' , 'English' ); studentQuery.matchesKeyInQuery ('nationality' , 'name' , countryQuery); studentQuery.find ().then ((students ) => { }); const query = new AV .Query ('Todo' );query.select (['title' , 'content' ]); query.first ().then ((todo ) => { const title = todo.get ('title' ); const content = todo.get ('content' ); const notes = todo.get ('notes' ); }); const query = new AV .Query ('Todo' );query.equalTo ('isComplete' , true ); query.count ().then ((count ) => { console .log (`${count} 个 todo 已完成。` ); }); const query = new AV .Query ('Comment' );query.limit (10 ); query.include ('post' ); query.find ().then ((comments ) => { comments.forEach ((comment ) => { const post = comment.get ('post' ); }); });
关联查询 创建关联 方法 1:设置 parent 字段,是指针吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 const post = new AV .Object ('Post' );post.set ('title' , '饿了……' ); post.set ('content' , '中午去哪吃呢?' ); const comment = new AV .Object ('Comment' );comment.set ('content' , '当然是肯德基啦!' ); comment.set ('parent' , post); comment.save ();
方法 2:使用 createWithoutData 获取指针(建议)
1 2 3 4 5 6 7 8 const post = AV .Object .createWithoutData ('Post' , '57328ca079bc44005c2472d0' );const comment = new AV .Object ('Comment' );comment.set ('content' , '当然是肯德基啦!' ); comment.set ('post' , post);
查询关联(基于方法 2) 1 2 3 4 5 6 const query = new AV .Query ('Comment' );query.equalTo ('post' , post); query.find ().then ((comments ) => { });
1 2 3 4 5 6 7 8 const innerQuery = new AV .Query ('Post' );innerQuery.exists ('image' ); const query = new AV .Query ('Comment' );query.matchesQuery ('post' , innerQuery);
多对多(重要!) 1 2 3 4 5 6 7 8 9 10 11 12 var studentTom = new AV .Object ('Student' );studentTom.set ('name' , 'Tom' ); var courseLinearAlgebra = AV .Object .createWithoutData ('Course' , '线性代数' );var studentCourseMapTom = new AV .Object ('StudentCourseMap' );studentCourseMapTom.set ('student' , studentTom); studentCourseMapTom.set ('course' , courseLinearAlgebra); studentCourseMapTom.save ();
同步对象 1 2 3 4 5 6 7 8 9 10 11 const todo = AV .Object .createWithoutData ('Todo' , '582570f38ac247004f39c24b' );todo.fetch ().then ((todo ) => { }); todo.fetch ({ keys : 'priority, location' }).then ((todo ) => { });
更新对象 1 2 3 4 const todo = AV .Object .createWithoutData ('Todo' , '582570f38ac247004f39c24b' );todo.set ('content' , '这周周会改到周三下午三点。' ); todo.save ();
有条件更新对象 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 const account = AV .Object .createWithoutData ('Account' , '5745557f71cfe40068c6abe0' );const amount = -100 ;account.increment ('balance' , amount); account.save (null , { query : new AV .Query ('Account' ).greaterThanOrEqualTo ('balance' , -amount), fetchWhenSave : true }).then ((account ) => { console .log (`当前余额为:${account.get('balance' )} ` ); }, (error ) => { if (error.code === 305 ) { console .error ('余额不足,操作失败!' ); } });
原子计数器 可以通过原子操作来增加或减少一个属性内保存的数字,用于多个客户端同时修改一个数字
1 post.increment ('likes' , 1 );
原子数组操作 更新数组也是原子操作。使用以下方法可以方便地维护数组类型的数据:
1 2 3 4 5 6 7 const todo = new AV .Object ('Todo' );todo.add ('arrayKey' , value) todo.addUnique ('arrayKey' , value) todo.remove ('arrayKey' , value)
如果你想导出数据/批量新增数据供 App 读取/备份 RESTAPI
权限管理
设置好各个 Class 的增删改查的权限
使用 masterKey 来操作后台数据
1 2 3 4 5 6 7 8 AV .init ({ appId : "abccc" , appKey : "abc" , serverURL : "https://xxx.com" , masterKey : "xxx" }); AV .debug .enable ();AV ._config .useMasterKey = true ;