简体   繁体   English

(WebPack) TypeError: Object is not a function or its return value is not iterable

[英](WebPack) TypeError: Object is not a function or its return value is not iterable

I have this function buildUpdateExpression that I use in the following Serverless handler and Webpack is returning me a TypeError as seen above directly related to said function as far as the stack trace goes...我有这个 function buildUpdateExpression ,我在以下Serverless处理程序中使用它,并且 Webpack 返回一个TypeError ,如上所示,与所述 ZC1C425268E68385D1AB45074C17A94 直接相关...

TypeError: Object is not a function or its return value is not iterable
    at .../update-user.js:7:80
    at .../handler-lib.js:4:19

1 import handler from "./libs/handler-lib";
2 // import dynamoDb from "./libs/dynamodb-lib";
3 import buildUpdateExpression from "./libs/utility-lib";
4 export const main = handler(async (event, context) => {
5  const data = JSON.parse(event.body);
6  const validExpressionAttributes = ["bio", "location", "profile-picture-link", "website"];
7  const [updateExpression, expressionAttributeValues] = buildUpdateExpression(validExpressionAttributes, data);
  return {
    updateExpression: updateExpression,
    expressionAttributeValues: expressionAttributeValues
  };
  // const params = {
  //   TableName: process.env.userTableName,
  //   Key: {
  //     "userId": event.requestContext.identity.cognitoIdentityId
  //   },
  //   ConditionExpression: "userId = :userIdVal",
  //   UpdateExpression: updateExpression,
  //   ExpressionAttributeValues: expressionAttributeValues,
  //   ReturnValues: "UPDATED_NEW"
  // };
  // await dynamoDb.update(params);
  // return params.Item;
});
>> libs/utility-lib.js
export function buildUpdateExpression(validExpressionAttributes, data) {
  const dataLength = Object.keys(data).length;
  let updateExpression = "set ";
  let expressionAttributeValues = {};
  for(let i = 0; i < validExpressionAttributes.length; i += 1) {
    let attribute = validExpressionAttributes[i];
    if(attribute in data) {
      let valueKey = ":" + attribute + "Val";
      expressionAttributeValues[valueKey] = data[attribute];
      updateExpression += attribute + " = " + valueKey;
      if(i < validExpressionAttributes.length - 1 && i < dataLength - 1) {
        updateExpression += ", ";
      }
    }
  }
  return [updateExpression, expressionAttributeValues];
};

I have unit tests that succeed when I do npm test当我进行npm test

import { buildUpdateExpression } from "../libs/utility-lib";
test("buildUpdateExpression (1)", () => {
  // Input variables
  const validExpressionAttributes = ["a", "b", "c"];
  const data = {
    a: 1,
    b: 2,
    c: 3
  };
  // Expected output variables
  const expectedUpdateExpression = "set a = :aVal, b = :bVal, c = :cVal";
  const expectedExpressionAttributeValues = {
    ":aVal": 1,
    ":bVal": 2,
    ":cVal": 3,
  };
  const [updateExpression, expressionAttributeValues] =
  buildUpdateExpression(validExpressionAttributes, data);
  expect(updateExpression).toEqual(expectedUpdateExpression);
  expect(expressionAttributeValues).toEqual(expectedExpressionAttributeValues);
});
 PASS  tests/utility-lib.test.js
  ✓ buildUpdateExpression (1) (3ms)
  ✓ buildUpdateExpression (2)
  ✓ buildUpdateExpression (3) (1ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        1.597s
Ran all test suites.

What could be the problem here?这里可能是什么问题?

The fix was that instead of importing as import buildUpdateExpression from "./libs/utility-lib";解决方法是import buildUpdateExpression from "./libs/utility-lib"; you must import as import { buildUpdateExpression } from "./libs/utility-lib";您必须import { buildUpdateExpression } from "./libs/utility-lib"; My suspicion is that the former imports a module whereas the latter imports the function... That's the only explanation I can think of and I'm probably wrong...我的怀疑是前者导入了一个模块,而后者导入了 function ......这是我能想到的唯一解释,我可能错了......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError: react__WEBPACK_IMPORTED_MODULE_1___default 不是 function 或其返回值不可迭代 - TypeError: react__WEBPACK_IMPORTED_MODULE_1___default is not a function or its return value is not iterable 类型错误:object 不是 function 或其返回值在反应 16.13.1 中不可迭代 - Typerror: object is not a function or its return value is not iterable in react 16.13.1 打字稿-“…不是函数或其返回值不可迭代” - Typescript - “… is not a function or its return value is not iterable” useContext 不是 function 或其返回值不可迭代 - useContext is not a function or its return value is not iterable “'x' is not a function or its return value is not iterable”的含义错误 - The meaning of “'x' is not a function or its return value is not iterable” error Firebase:snapshot.val不是函数或其返回值不可迭代 - Firebase: snapshot.val is not a function or its return value is not iterable webpack 4 TypeError:“ Object(…)不是函数” - webpack 4 TypeError: “Object(…) is not a function” TypeError:“未定义”对象是否不可迭代? - TypeError: 'Undefined' object is not iterable? 奇怪的(Webpack?)错误“ TypeError:Object(…)不是函数” - Strange (webpack?) error “TypeError: Object(…) is not a function” Uncaught TypeError: Object(...) is not a function 当与 WebPack 4 捆绑时 - Uncaught TypeError: Object(...) is not a function when bundling with WebPack 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM