预期 'property' 是字符串类型,而不是找到类型对象 - Dynamoose

Expected 'property' to be of type string, instead found type object - Dynamoose

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

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

这是我的代码:

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 错误仅适用于此型号,相同的代码适用于其他型号 table。

控制台错误

我的Table

这似乎与此有关 github issue on Dyanmoose

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

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

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

这是 Document 之前的样子:

执行上述代码后:

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

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));

您报告的错误是根据您在 checkTypeFunction:

中的模式模型检查返回的 Item 类型时发生的更改的结果
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}.`);
...

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

架构必须是这样的:

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,
});

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

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

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

能否请您尝试实施以下 async/await-structure 并告诉我这是否有帮助:

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);
    }
});