创建元素 sails-dynamodb 时类型不匹配

Type mismatch on create element sails-dynamodb

我正在使用这个模块,当我尝试创建一个 Object Sails 时,请返回给我 ("Type mismatch for attribute to update")

这是我的代码:

型号:

module.exports = {
 attributes: {
  id:{
  type: 'string',
  primaryKey: 'range'
 },
 picture:{
  type: "string",
  required: true
 },
 title:{
  type: "string",
  required: true
 },
 subcategories:{
  collection: 'Subcategory',
  via: 'category_ref'
 },
 user_ref:{
  model: 'User'
 }
}
};

控制器:

create: function (req, res, next) {
       let  name = "sometext";
        var obj = {
            id: new String(uuidv4()),
            picture: name,
            title: req.param('title')
        }
        Category.create(obj, function (err, cat) {
            if (err) {
                return next(err);
            } else {
                return res.send(cat);
            }
        });
    });
},

我用 instanceof 验证了它是一个字符串。

我的 Sails 版本是 0.12.14。

提前致谢

我很确定问题出在您正在使用 new String(uuidv4())。当您将 new 与原始构造函数(如 String、Number 或 Boolean)一起使用时,构造函数 returns 是一个对象。这不是你想要的。相反,删除 new 并像任何普通函数一样调用构造函数,例如String(uuidv4())。以这种方式调用时,构造函数 returns 是一个原语。

下面的例子应该能让您了解其中的区别

> new String('test') instanceof String
true
> typeof new String('test')
'object'
> String('test') instanceof String
false
> typeof String('test')
'string'
> new String('test') === 'test'
false
> String('test') === 'test'
true

还有一些其他事情很突出。 id 被定义为模型上的范围键,并且没有模型的哈希键。此外,req.param('title') 可能会返回未定义、空值或非字符串的内容。