11

我正在使用AWS DynamoDBDynamoose尝试使用Scan函数获取记录,但遇到了一个我无法识别的问题。

奇怪的是,它能够以相同的方式从另一个表中获取记录并成功获取记录。

这是我的代码:

const vehicleMasterSchema = new dynamoose.Schema({
    "id": String,
    "customer_account_number": String,
    "fuel_type": String,
    "make": String,
    "model": String,
    "odometer_gatex": String,
    "plate_no": String,
    "rfid_gatex": String,
    "sales_agreement_id": String,
    "vehicle_category": String,
    "vehicle_id": String,
}, {
    "timestamps": {
        "createdAt": "create_date",
        "updatedAt": null // updatedAt will not be stored as part of the timestamp
    }
});
const vehicleMasterModel = dynamoose.model("vehicle_master", vehicleMasterSchema, { "create": false });

router.post('/getFuelingStatus', (req, res) => {
    var companyInfo = req.body;
    try {
        console.log(typeof vehicleMasterModel);
        vehicleMasterModel.scan("customer_account_number").eq(companyInfo.customerId).exec((error, results) => {
            if (error) {
                console.error(error);
            } else {
                res.json(results);
            }
        });
    } catch (error) {
        res.json(error);
    }
});

TypeMismatch错误仅针对此模型出现,相同的代码适用于另一个表。

控制台错误

控制台错误

我的桌子

表数据

这似乎与Dyanmoose 上的这个 github 问题有关

4

3 回答 3

4

我的猜测是问题可能与您的属性名称有关,model.

事实上,这是实际情况:从源代码中提取的以下代码Document.ts是覆盖您的model属性的代码:

Object.defineProperty(this, "model", {
  "configurable": false,
  "value": model
});

这是Document之前的样子:

之前的文件

在执行上述代码之后:

之后的文件

此代码在处理Scan exec函数时执行,DocumentRetriever.ts当库将ItemDynamoDB 返回的每个函数映射到它们的内部Document表示时,正好在这行代码中:

const array: any = (await Promise.all(result.Items.map(async (item) => await new this.internalSettings.model.Document(item, {"type": "fromDynamo"}).conformToSchema({"customTypesDynamo": true, "checkExpiredItem": true, "saveUnknown": true, "modifiers": ["get"], "type": "fromDynamo"})))).filter((a) => Boolean(a));

Item您报告的错误是根据您的模式模型检查返回的类型时更改的结果checkTypeFunction

const {isValidType, matchedTypeDetails, typeDetailsArray} = utils.dynamoose.getValueTypeCheckResult(schema, value, genericKey, settings, {"standardKey": true, typeIndexOptionMap});
if (!isValidType) {
  throw new Error.TypeMismatch(`Expected ${key} to be of type ${typeDetailsArray.map((detail) => detail.dynamicName ? detail.dynamicName() : detail.name.toLowerCase()).join(", ")}, instead found type ${typeof value}.`);
...

请尝试不同的名称,我认为它会正常工作。

于 2020-11-12T22:12:49.330 回答
1

架构必须是这样的:

const ImageGalleryFoldersSchema = new Schema({
  key: {
    type: String,
    hashKey: true,
    required: true,
  },
  displayName: {
    type: String,
    required: true,
  },
  parentFolderKey: {
    type: String,
    required: false,
  },
  isActive: {
    type: Boolean,
    default: true,
    required: false,
  },
}, {
  timestamps: true,
});
于 2020-11-14T17:21:54.017 回答
0

也许您的问题是由于异步行为引起的。

更具体地说,我认为当您调用“扫描”功能链时,主体请求尚未完成。但是,由于提升的性质,在您进入函数调用之前,对象“companyInfo”已经被初始化。

因此,您可能会收到指定的“TypeMismatch”错误。

您能否尝试实现以下异步/等待结构并告诉我这是否有帮助:

router.post('/getFuelingStatus', async (req, res) => {
    var companyInfo = await req.body;
    try {
        console.log(typeof vehicleMasterModel);
        vehicleMasterModel.scan("customer_account_number").eq(companyInfo.customerId).exec((error, results) => {
            if (error) {
                console.error(error);
            } else {
                res.json(results);
            }
        });
    } catch (error) {
        res.json(error);
    }
});
于 2020-11-19T15:39:08.480 回答