开发者

How to retrieve mongo db object and return it as json object

开发者 https://www.devze.com 2023-03-09 20:57 出处:网络
Hi I have this as one of my controllers: [HttpPost] public JsonResult GetPinPoints(string Id) { Frames rslt = null;

Hi I have this as one of my controllers:

[HttpPost]
        public JsonResult GetPinPoints(string Id)
    {
        Frames rslt = null;

        string connString = ConfigurationManager.ConnectionStrings["MongoConnStringNew"].ToString();
        MongoUrl murl = new MongoUrl(connString);
        MongoServer mgconf = new MongoServer(murl);
        try
        {
            mgconf.Connect();
            MongoDatabase frmlydb = mgconf.GetDatabase("framely");
            MongoCollection<Frames> collection = frmlydb.GetCollection<Frames>("Frames");
            ObjectId oid = new ObjectId(Id);
            Frames frms = collection.FindOne(Query.EQ("_id", oid));

            if (frms != null)
            {
                rslt = frms;
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            mgconf.Disconnect();
        }

        return Json(rslt.CoordinatesObj.ToJson());
    }

The mongo object looks like this:

{"MetaTagsObj":{"Meta1":"my fam","Meta2":"lololo","Meta3":"lulz"},"PictureID":"http://framely.s3.amazonaws.com/0b7a9a72-c61b-4dec-a814-40b003072e31.jpg","UserID":"1","CoordinatesObj":[{"Position":1,"Top":182,"Left":20,"Height":73,"Width":90},{"Position":2,"Top":69,"Left":103,"Height":98,"Width":1...

I use an ajax jquery function to call the controller that looks like this:

$("#mybutton").click(function(){
           $.ajax({
                url: '/Member/GetPinPoints',
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function(data) {
            alert(data);
            开发者_开发知识库},
            error: function() {

            alert("error");
            }
            });


    return false;
});

I dont think I am doing this right, I think it has to do with the way I return the json object. I keep getting this error:

{"Object reference not set to an instance of an object."}

right where I am returning the Json object.


The return Json(myObject) statement should take an object that'll be serialised to JSON then returned to the browser as a string, but by calling ToJson() the rslt.CoordinatesObj object will be serialised twice.

It's also possible CoordinatesObj isn't being deserislised properly, so it's throwing an exception because ToJson() is called on a null object. The Frames class should look something like this to handle deserialising the CoordinatesObj array:

public class Frames
{
    IEnumerable<Coordinate> CoordinatesObj { get; set; }

    public class Coordinate
    {
        int Position { get; set; }
        int Top { get; set; }
        int Left { get; set; }
        int Height { get; set; }
        int Width { get; set; }
    }
}


If FindOne doesn't find a matching document it returns null, so in your code sample it is entirely possible for the "rslt" variable to be null.

Also:

  1. no need to call Connect, the driver connects automatically
  2. dont' call Disconnect, it interferes with connection pooling
0

精彩评论

暂无评论...
验证码 换一张
取 消