如何在mongodb中保存ParseGeoPoint对象(使用mLab) [英] How can i save a ParseGeoPoint Object in mongodb (using mLab)

查看:143
本文介绍了如何在mongodb中保存ParseGeoPoint对象(使用mLab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用地理点保存地址(关系对象),以便 以后检索所有地址接近提供的事件 位置,将全部基于坐标.使用以下作品 代码-

I want to save address ( a relational object ) with geopoints, so as to later retrieve all Events which have address near to provided location, it will be all based on coordinates. Using following piece of code -

$address = new ParseObject("Address");
$point = new ParseGeoPoint(23.80, -120.90);
$address->set("location", $point);
$address->save();

我数据库中的位置类型是geopoint,但它使我连续 错误-

Type of location in my db is geopoint, but it gives me continuous error-

内部服务器错误.错误代码:异常"Exception"和消息无法序列化未保存的Parse.Object"

我尝试过以下代码-

$address = new ParseObject("Address");
$address->save();
$point = new ParseGeoPoint(23.80, -120.90);
$address->set("location", $point);
$address->save();

我尝试过此操作,以消除未保存的Parse.Object的上述错误,但是 最终只保存了我的地址对象,而从不保存位置 它.我该如何实现?

I tried this, so as to remove above error of unsaved Parse.Object, but it ended up saving only my address object and never saving location in it. How can I achieve this?

完整代码视图-

function saveEvent() {
   $title = $this->input->post('title');
   $eventCode = $this->input->post('eventCode');
   $event = new ParseObject("Event");
   $event->set("title", $title);
   $event->set("eventCode", $eventCode);
   $address = $this->saveAddressObject($event, $url);
   $event_address_Relation = $event->getRelation("address");
   if (!empty($address)) {
            $event_address_Relation->add($address);
   }
   $event->save();
  }
function saveAddressObject($event, $url) {
    $fullAddress = $this->input->post('fullAddress');
    $address1 = $this->input->post('address1');
    $address2 = $this->input->post('address2');
    $city = $this->input->post('city');
    $state = $this->input->post('state');
    $zipCode = (int) $this->input->post('zipCode');
    $country = $this->input->post('country');
    $latitude = $this->input->post('latitude');
    $longitude = $this->input->post('longitude');
    try {
            $address = new ParseObject("Address");
            $address->set("fullAddress", $fullAddress);
            $address->set("address1", $address1);
            $address->set("address2", $address2);
            $address->set("city", $city);
            $address->set("state", $state);
            $address->set("zipCode", $zipCode);
            $address->set("country", $country);
            $address->set("latitude", $latitude);
            $address->set("longitude", $longitude);
            $point = new ParseGeoPoint($latitude, $longitude);
            $address->set("location", $point);
            $address->save();
           return $address;
    } catch (ParseException $error) {
        print_r($error);
    } catch (Exception $ex1) {
        print_r($ex1);
    }
}

推荐答案

对于您的第二个示例,一次只能保存一个对象的实例,而在进行另一次尝试时进行的第二次以上尝试会静默失败.至少在iOS上.我会想象这里很相似.

For your second example, only one instance of an object can be saved at a time, and the second+ attempts while another attempt is in progress fail silently. At least on iOS. I would imagine it's similar here.

这是您的确切代码吗?当您尝试将尚未保存的新对象附加到另一个对象时,您提到的第一个错误往往会发生,并且您提到这些地址是按关系使用的.我敢打赌,您正在尝试将一个对象作为指针附加到另一个对象,并且尚未保存该对象,这意味着该对象尚没有ID,因此无法创建指向该对象的指针.

Is this your exact code? The first error you mentioned tends to happen when you try to attach a new object that hasn't been saved yet to another object, and you mentioned that these addresses are used relationally. My bet is that you're trying to attach an object to another as a pointer, and haven't saved the object, which means it does not yet have an ID so you can't create a pointer to it.

编辑-我不确定您使用的是哪个SDK,但是保存调用是异步的,这意味着它不会立即发生,因此您可能认为您首先要保存一个对象,但实际上保存还没有. t完成,然后尝试将其附加到另一个对象?这也可能与第二个示例失败的原因有关,因为您没有第二次保存后返回的完成处理程序.也许第一个成功完成,但是第二个从来没有在函数返回之前完成.

Edit - I'm not sure which SDK you're using, but save calls are asynchronous, meaning it doesn't happen right away, so possibly you think you're saving an object first, but really the save hasn't finished, and you then try to attach it to another object? This could also be related to why your second example fails, since you don't have any completion handler that returns after the second save. Perhaps the first one manages to finish, but the second one never does before your function returns.

这篇关于如何在mongodb中保存ParseGeoPoint对象(使用mLab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆