云服务器-leancloud

云服务器--leancloud

leancloud貌似是中国最早开始做云服务器的,对于开发移动端的人来说终于可以不用苦苦等待服务端的数据

,而作为另外一种选择调试移动端的代码。

我用的是JS的,主要说说JS的一些问题。

1,快速入门:https://leancloud.cn/start.html

2,注意引用此js时,要放到最前面,否则可能不起作用

<script src="https://leancloud.cn/scripts/lib/av-0.5.0.min.js"></script>

3,测试数据正常后,你可以在它的云服务器数据库看到刚上传的数据(在你应用的存储分类里可以看到)

4,保存数据的方法 https://leancloud.cn/docs/js_guide.html#保存对象

var gameScore = new GameScore();
gameScore.set("score", 1337);
gameScore.set("playerName", "Sean Plott");
gameScore.set("cheatMode", false);
gameScore.save(null, {
  success: function(gameScore) {
    // Execute any logic that should take place after the object is saved.
    alert('New object created with objectId: ' + gameScore.id);
  },
  error: function(gameScore, error) {
    // Execute any logic that should take place if the save fails.
    // error is a AV.Error with an error code and description.
    alert('Failed to create new object, with error code: ' + error.description);
  }
});

官方文档的示例代码有一点问题,如果这么写error.description,会提示description未定义,看api才知道是message

还有保存数据的键值不能为id,否则不能保存成功

5,查询数据的方法 https://leancloud.cn/docs/js_guide.html#检索对象

(不知道为什么官方文档比我前几天看又少了好像,只说了查询id的方法,应该还有可以设置查询条件的)

var Requires = AV.Object.extend("Requires");
    
    var updateStatus  = function(updateid,groupid){
    	$ionicLoading.show({
            template: 'Loading...'
        });
       var query = new AV.Query(Requires);
       query.equalTo("requireId", updateid);
       query.find({
	       success: function(results) {
		   $ionicLoading.hide();

		   var object = results[0];

		   object.set("groupId", groupid);

		   object.save();
		   $cordovaToast.show('更新需求状态成功', 'long', 'bottom');
	       },
	       error: function(error) {
	       	   $ionicLoading.hide();
		       alert("Error: " + error.code + " " + error.message);
	       }
        });
    }
不设置查询条件query.equalTo的话就会获取到整个表的数据

6,更新数据的方法 https://leancloud.cn/docs/js_guide.html#更新对象

(官方文档太误导人了,写了两次保存,其实应该是查询出对象,更改数据再保存

,不知道怎么想的,这个有点严重,说实话一开始为什么要摆个那样的例子放那呢云服务器-leancloud


官方示例代码

// Create the object.
var gameScore = new GameScore();

gameScore.set("score", 1337);
gameScore.set("playerName", "Sean Plott");
gameScore.set("cheatMode", false);
gameScore.set("skills", ["pwnage", "flying"]);

gameScore.save(null, {
  success: function(gameScore) {
    // Now let's update it with some new data. In this case, only cheatMode and score
    // will get sent to the cloud. playerName hasn't changed.
    gameScore.set("cheatMode", true);
    gameScore.set("score", 1338);
    gameScore.save();
  }
});


我的代码

 var Requires = AV.Object.extend("Requires");
                        var require = new Requires();
			var query = new AV.Query(Requires);
			query.equalTo("requireId", parseInt(todoid));
			query.find({
			success: function(results) {
			
			var object = results[0];
						
                        object.set("groupId", $scope.task_groupid);
                        object.set("importance", $scope.priority_level);
                        object.set("classname", $scope.classname);
                        object.set("title", $scope.task_title);
            
                        object.set("detail", $scope.task_detail);
                        object.set("images", $scope.images_list);
          
                        object.set("record", $scope.recordPath);
                        object.set("donedate", $scope.done_date);
                        object.save();
                        
			},
			error: function(error) {
						
			      alert("Error: " + error.code + " " + error.message);
			    }
			});