1

这是我的模型定义:

const uuid4 = require('uuid4');

const dynamooseClient = require('./index').dynamoose;


const supermarketShoppingCart = new dynamooseClient.Schema({
    cartId: { 
        type: Number, 
        required: true, 
        hashKey: true,
        default: uuid4
     },
     userId: {
        type: String,
        required: true
     },
     products: {
         type: 'Map',
         map: {
            productId: { type: String, required: true },
            quantity: Number,
            unitaryPrice: Number,
            subtotal: Number, 
            discount: Number,
            totalBeforeTax: Number,
            isTaxAplicable: Boolean,
            unitaryTax: Number,
            totalTax: Number
         }
     },
}, { useDocumentTypes: true });

module.exports = dynamooseClient.model('supermarketShoppingCart', supermarketShoppingCart);

这是我的发帖请求:

app.post("/shoppingCart", (req, res) => {
  let shoppingCart = new supermarketShoppingCart({
    userId: req.body.userId,
    products: [
    {
        productId: req.body.products.productId
    }
    ]
  });
  shoppingCart
    .save()
    .then(cart => {
      console.log(cart);
    })
    .catch(error => {
      console.log(error);
    });
});

但是,它会引发以下错误:

{ ValidationError: Required value missing: productId
    at Attribute.toDynamo (C:\www\sm_saas\kiwik-api\node_modules\dynamoose\lib\Attribute.js:295:13)
    at Attribute.toDynamo (C:\www\sm_saas\kiwik-api\node_modules\dynamoose\lib\Attribute.js:350:29)
    at Schema.toDynamo (C:\www\sm_saas\kiwik-api\node_modules\dynamoose\lib\Schema.js:155:25)
    at NewModel.Model.put (C:\www\sm_saas\kiwik-api\node_modules\dynamoose\lib\Model.js:275:20)
    at app.post (C:\www\sm_saas\kiwik-api\app.js:28:6)
    at Layer.handle [as handle_request] (C:\www\sm_saas\kiwik-api\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\www\sm_saas\kiwik-api\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\www\sm_saas\kiwik-api\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\www\sm_saas\kiwik-api\node_modules\express\lib\router\layer.js:95:5)
    at C:\www\sm_saas\kiwik-api\node_modules\express\lib\router\index.js:281:22
  name: 'ValidationError',
  message: 'Required value missing: productId' }

任何人都知道我到底做错了什么?这是我通过 POSTMAN 发送的 JSON:

{
  "userId": "hectorsoto07@gmail.com",
  "products": [
    {
      "productId": "123123"
    }
  ]
}
4

0 回答 0