How can i save a ParseGeoPoint Object in mongodb (

2019-08-23 11:13发布

问题:

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();

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

Internal Server Error . Error code :exception 'Exception' with message 'Can't serialize an unsaved Parse.Object'

Post this I tried following code -

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

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?

Full code view -

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);
    }
}

回答1:

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.

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.

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.