Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 响应重复,但计数显示为1_Loops_Serverless Framework_Dynamoose - Fatal编程技术网

Loops 响应重复,但计数显示为1

Loops 响应重复,但计数显示为1,loops,serverless-framework,dynamoose,Loops,Serverless Framework,Dynamoose,使用无服务器的Dynamoose ORM。我有一个场景,根据推荐查找用户信息 答复如下 { "data": { "results": [ { "specialTip": "Hello World", "recommendation": "Huli ka!", "poi": { "uuid": "poi_555",

使用无服务器的Dynamoose ORM。我有一个场景,根据推荐查找用户信息

答复如下

{
    "data": {
        "results": [
            {
                "specialTip": "Hello World",
                "recommendation": "Huli ka!",
                "poi": {
                    "uuid": "poi_555",
                    "name": "Bukit Panjang",
                    "images": [
                        {
                            "url": "facebook.com",
                            "libraryUuid": "2222",
                            "uuid": "9999"
                        }
                    ]
                },
                "uuid": "i_8253578c-600d-4dfd-bd40-ce5b9bb89067",
                "headline": "Awesome",
                "dataset": "attractions",
                "insiderUUID": "i_c932e85b-0aee-4462-b930-962f555b64bd",
                "insiderInfo": [
                    {
                        "gender": "m",
                        "funFacts": [
                            {
                                "type": "knock knock!",
                                "answer": "Who's there?"
                            }
                        ],
                        "profileImage": "newImage.jpg",
                        "shortDescription": "Samething",
                        "fullDescription": "Whatever Description",
                        "interests": [
                            "HELLO",
                            "WORLD"
                        ],
                        "tribes": [
                            "HELLO",
                            "WORLD"
                        ],
                        "uuid": "i_c932e85b-0aee-4462-b930-962f555b64bd",
                        "personalities": [
                            "HELLO",
                            "WORLD"
                        ],
                        "travelledCities": [
                            "HELLO",
                            "WORLD"
                        ]
                    }
                ]
            },
            {
                "specialTip": "Hello World",
                "recommendation": "Huli ka!",
                "poi": {
                    "uuid": "poi_555",
                    "name": "Bukit Panjang",
                    "images": [
                        {
                            "url": "facebook.com",
                            "libraryUuid": "2222",
                            "uuid": "9999"
                        }
                    ]
                },
                "uuid": "i_8253578c-600d-4dfd-bd40-ce5b9bb89067",
                "headline": "Awesome",
                "dataset": "attractions",
                "insiderUUID": "i_c932e85b-0aee-4462-b930-962f555b64bd",
                "insiderInfo": [
                    {
                        "gender": "m",
                        "funFacts": [
                            {
                                "type": "knock knock!",
                                "answer": "Who's there?"
                            }
                        ],
                        "profileImage": "newImage.jpg",
                        "shortDescription": "Samething",
                        "fullDescription": "Whatever Description",
                        "interests": [
                            "HELLO",
                            "WORLD"
                        ],
                        "tribes": [
                            "HELLO",
                            "WORLD"
                        ],
                        "uuid": "i_c932e85b-0aee-4462-b930-962f555b64bd",
                        "personalities": [
                            "HELLO",
                            "WORLD"
                        ],
                        "travelledCities": [
                            "HELLO",
                            "WORLD"
                        ]
                    }
                ]
            }
        ],
        "count": 1
    },
    "statusCode": 200
}
不确定哪里出错,因为响应中的项目似乎重复,但计数为1

这是密码

module.exports.index = (_event, _context, callback) => {

  Recommendation.scan().exec((_err, recommendations) => {
    if (recommendations.count == 0) {
      return;
    }
    let results = [];
    recommendations.forEach((recommendation) => {
      Insider.query({uuid: recommendation.insiderUUID}).exec((_err, insider) => {
        if (insider.count == 0) {
          return;
        }
        recommendation.insiderInfo = insider;
        results.push(recommendation);
      });
    });
    const response = {
      data: {
        results: results,
        count: results.count
      },
      statusCode: 200
    };
    callback(null, response);
  });
};

编辑:我以前的代码忽略了“内部人”查询是异步的这一事实。这段新代码将处理该问题,并与您的编辑匹配

const async = require('async'); // install async with 'npm install --save async'
[...]
module.exports.index = (_event, _context, callback) => {

  Recommendation.scan().exec((_err, recommendations) => {
    if (_err) {
      console.log(_err);
      return callback(_err);
    }
    if (recommendations.count == 0) {
      const response = {
        data: {
          results: [],
          count: 0
        },
        statusCode: 200
      };
      return callback(null, response);
    }

    let results = [];
    async.each(recommendations, (recommendation, cb) => { // We need to handle each recommendation asynchronously...
      Insider.query({uuid: recommendation.insiderUUID}).exec((_err, insider) => { // because this is asynchronous
        if (_err) {
          console.log(_err);
          return callback(_err);
        }
        if (insider.count == 0) {
          return cb(null);
        }
        recommendation.insiderInfo = insider;
        results.push(recommendation);
        return cb(null);
      });
    }, (err) => { // Once all items are handled, this is called
      if (err) {
        console.log(err);
        return callback(err);
      }
      const response = { // We prepare our response
        data: {
          results: results, // Results may be in a different order than in the initial `recommendations` array
          count: results.count
        },
        statusCode: 200
      };
      callback(null, response); // We call our main callback only once
    });
  });
};

初始(部分错误)答案,供参考。
您正在将映射结果推送到当前映射的对象中,并且在此处多次调用了
回调。这是相当多的意外行为材料。
请尝试以下操作:

let results = [];
recommendations.forEach((recommendation) => {
    Insider.query({uuid: recommendation.insiderUUID}).exec((_err, insider) => {
        if (insider.count == 0) {
          return;
        }
        recommendation.insiderInfo = insider;
        results.push(recommendation);
    });
});    
let response = {
  data: {
    results: results,
    count: results.count
  },
  statusCode: 200
};
callback(null, response);

编辑:我以前的代码忽略了“内部人”查询是异步的这一事实。这段新代码将处理该问题,并与您的编辑匹配

const async = require('async'); // install async with 'npm install --save async'
[...]
module.exports.index = (_event, _context, callback) => {

  Recommendation.scan().exec((_err, recommendations) => {
    if (_err) {
      console.log(_err);
      return callback(_err);
    }
    if (recommendations.count == 0) {
      const response = {
        data: {
          results: [],
          count: 0
        },
        statusCode: 200
      };
      return callback(null, response);
    }

    let results = [];
    async.each(recommendations, (recommendation, cb) => { // We need to handle each recommendation asynchronously...
      Insider.query({uuid: recommendation.insiderUUID}).exec((_err, insider) => { // because this is asynchronous
        if (_err) {
          console.log(_err);
          return callback(_err);
        }
        if (insider.count == 0) {
          return cb(null);
        }
        recommendation.insiderInfo = insider;
        results.push(recommendation);
        return cb(null);
      });
    }, (err) => { // Once all items are handled, this is called
      if (err) {
        console.log(err);
        return callback(err);
      }
      const response = { // We prepare our response
        data: {
          results: results, // Results may be in a different order than in the initial `recommendations` array
          count: results.count
        },
        statusCode: 200
      };
      callback(null, response); // We call our main callback only once
    });
  });
};

初始(部分错误)答案,供参考。
您正在将映射结果推送到当前映射的对象中,并且在此处多次调用了
回调。这是相当多的意外行为材料。
请尝试以下操作:

let results = [];
recommendations.forEach((recommendation) => {
    Insider.query({uuid: recommendation.insiderUUID}).exec((_err, insider) => {
        if (insider.count == 0) {
          return;
        }
        recommendation.insiderInfo = insider;
        results.push(recommendation);
    });
});    
let response = {
  data: {
    results: results,
    count: results.count
  },
  statusCode: 200
};
callback(null, response);

感谢您指出我的错误,将其推回映射对象。虽然,在我做了更改之后,现在
results=>[]
。您是否使用了我编写的代码?如果没有,给我们看新代码。我更新了答案。我错过了代码中的异步请求。现在应该可以了。我对代码做了一些修改,但是你提供的代码也可以。谢谢。谢谢你指出我的错误,把它推回到映射对象。虽然,在我做了更改之后,现在
results=>[]
。您是否使用了我编写的代码?如果没有,给我们看新代码。我更新了答案。我错过了代码中的异步请求。现在应该可以了。我对代码做了一些修改,但是你提供的代码也可以。非常感谢。