使用ISODate对象mongoDB解析JSON Post - Node.js

时间:2015-03-21 04:10:54

标签: objective-c json mongodb

我正在尝试使用ISODate将JSON文档解析为mongodb ...但字段是字符串而不是ISODate。如何在不更改我的Node.JS post方法的情况下使用ISODate发送JSON,该方法接收JSON并保存在我的集合中

   - (NSDictionary*) toDictionary
{
    NSMutableDictionary* jsonable = [NSMutableDictionary dictionary];
    NSString *dataEventoISO = [NSString stringWithFormat:@"ISODate(%@)",self.dataEvento];
    safeSet(jsonable, @"name", self.name);
    safeSet(jsonable, @"placename", self.placeName);
    safeSet(jsonable, @"location", self.location);
    safeSet(jsonable, @"details", self.details);
    safeSet(jsonable, @"imageId", self.imageId);
    safeSet(jsonable, @"categories", self.categories);
    safeSet(jsonable, @"_id", self._id);
    safeSet(jsonable, @"usuarioID", self.usuarioID);
    safeSet(jsonable, @"status", self.status);
    safeSet(jsonable, @"emailUsuario", self.emailUsuario);
    safeSet(jsonable, @"dataEvento", dataEventoISO);
    return jsonable;
}

和方法发布:

 - (void) persist:(Location*)location
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];

    if (!location || location.name == nil || location.name.length == 0) {
        return; //input safety check
    }

    //if there is an image, save it first
    if (location.image != nil && location.imageId == nil) { //1
        [self saveNewLocationImageFirst:location]; //2
        return;             
    }

    NSString* locations = [kBaseURL stringByAppendingPathComponent:kLocations];

    BOOL isExistingLocation = location._id != nil;

    if(!isExistingLocation)
        location.votos = @"0";

    NSString *urlStr = isExistingLocation ? [locations stringByAppendingPathComponent:location._id] : locations;
    NSURL* url = [NSURL URLWithString:urlStr]; //1

    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = isExistingLocation ? @"PUT" : @"POST"; //2
    location.usuarioID = appDelegate.usuarioLogado.identificador;

    if([location.status length] < 1 || [location.status isEqualToString:@"(null)"])
        location.status = @"0";
    location.emailUsuario = appDelegate.usuarioLogado.email;
    NSData* data = [NSJSONSerialization dataWithJSONObject:[location toDictionary] options:0 error:NULL]; //3

    request.HTTPBody = data;
       [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //4

    NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession* session = [NSURLSession sessionWithConfiguration:config];

    NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { //5
        if (!error) {

            NSArray* responseArray = @[[NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]];
           [self parseAndAddLocations:responseArray toArray:self.objects];
            dispatch_group_leave([AppDelegate appDelegate].grupo_persist);
        }
    }];
    [dataTask resume];

}

1 个答案:

答案 0 :(得分:0)

我编辑了我的REST API,效果很好!

 CollectionDriver.prototype.save = function(collectionName, obj, callback) {
    this.getCollection(collectionName, function(error, the_collection) { //A
      if( error ) callback(error)
      else {

       if(obj.dataEvento)
         obj.dataEvento = new Date(obj.dataEvento);
        the_collection.insert(obj, function() { //C
          callback(null, obj);
        });
      }
    });
};