方法返回ID,但不保存其他数据点

时间:2018-10-30 17:14:49

标签: javascript node.js typescript asynchronous mongoose

当我将值放在新Farm()中时,我的方法有效,但是当我通过JSON中的邮递员运行它时,该方法不起作用,这也在下面显示。我认为我没有错误地解析JSON。任何建议将不胜感激?

export interface IDataFlowService {
handle(request: IDataFlowRequest): Promise<IDataFlowResponse>;

}

export class DataFlowService implements IDataFlowService {

current_env = process.env.NODE_ENV;

 async handle(request: IDataFlowRequest): Promise<IDataFlowResponse> {

    let response: IDataFlowResponse;

    try {


        switch (request.requestType) {
            case "SaveFarms":
                response = await this.SaveFarms(request);
                break;

                default:
                winston.error(`Request failed for request type: ${request.requestType}`);

                break;
        }
        return response;

    } catch (e) {
        winston.error(`handle method failed for request: ${request}`, e);

        response = {
            success: false,
            error: {
                'message': e.message,
            }
        };
        return response;
    }
}

private async SaveFarms(request: IDataFlowRequest): Promise<IDataFlowResponse> {

    const response: IDataFlowResponse = {
        success: true ,
        farmIds: [],
        names: []
       };

for (const farm of request.farms) {

const newFarm: IFarmModel = new Farm();

Promise.all([newFarm.save()]);
response.farmIds.push(newFarm.farmId) ;
response.names.push(newFarm.name) ;

       }


        return response;
    }
}

这里是帖子:http://localhost:5000/api/dataflow

{
"requestType":"SaveFarms",
"farms": [

  {
  "name" : "Bronx"

   }

   ]
   }

如您所见,我收到响应,但是名称字段返回空/空:

{
"success": true,
"farmIds": [
    "fH1WjllXR"
],
"names": [
    null
]
}

1 个答案:

答案 0 :(得分:2)

您忘记为name对象设置newFarm

const newFarm: IFarmModel = new Farm();
newFarm.name = farm.name; // I think so