首页 > 解决方案 > 我在 SailsJs 上使用 waterline-orm 模型方法“.archive()”有一个“UsageError”

问题描述

当我尝试在 Sails 后端存档记录时,这是邮递员返回的错误。

UsageError: 新记录的初始数据无效。\n详细信息:\n 无法使用提供的新记录之一:所需属性缺少值id。应为字符串,但得到: undefined\n [?] 请参阅https://sailsjs.com/support以获取帮助

请问有人知道这个错误是什么意思吗?提前致谢。

感谢您的回复我的代码: - 在控制器中:

try {
      await Laboratory.archive({id: inputs.id});
    } catch (error) {
      console.log('Error-6 CORE-DELETE_LABORATORY:', error)
      exits.failed(`Error-6 CORE-DELETE_LABORATORY: ${error}${inputs.id}`)}

在文件 config/models.js 中:

module.exports.models = {

  migrate: 'alter',

  fetchRecordsOnUpdate: true,
  fetchRecordsOnCreate: true,
  fetchRecordsOnCreateEach: true,

  attributes: {

    createdAt: { type: 'ref', columnType: 'datetime', autoCreatedAt: true, },
    updatedAt: { type: 'ref', columnType: 'datetime', autoUpdatedAt: true, },

    id: { type: 'string', columnName: '_id' },
  },

  dataEncryptionKeys: {
    default: 'dRYQHBf8Zpza2vMS5BB3qcSiLspJP4BG37I2JkP2yYw='
  },

  cascadeOnDestroy: true
};

实验室型号:

module.exports = {

  attributes: {
    type: {
      type: 'string',
      isIn: ['INSIDE', 'OUTSIDE'],
      required: true,
    },
    raisonSocial: {
      type: 'string',
      required: true,
      unique: true,
    },

    city: {
      type: 'string'
    },

    address: {
      type: 'string'
    },

    email: {
      type: 'string'
    },

    neighborhood: {
      type: 'string'
    },

    contacts: {
      type: 'string'
    },

    logo: {
      type: 'string'
    },

    lat: {
      description: 'Latitude',
      type: 'number'
    },

    long: {
      description: 'Longitude',
      type: 'number'
    },

    website: {
      type: 'string',
      defaultsTo: ''
    },
    subdomain: {
      type: 'string',
      unique: true,
      required: true,
    },

    mobileMoney: {
      type: 'string'
    },

    bank: {
      type: 'string',
      defaultsTo: ''
    },
    bankAccountID: {
      type: 'string',
      defaultsTo: ''
    },

    validated : {
      type: 'boolean',
      defaultsTo : false
    },

    f_establishment: {
      model: 'establishment'
    },

    director: {
      description: 'Chef of laboratory id',
      type: 'json',
      example: '{name, phone, role}',
      required: true,
    }
  },

  customToJSON () {
    if (this.logo) {
      this.logo = `${process.env.BASE_URL}/avatar/${this.logo}`
    }
    return this
  }
};

谢谢。

标签: sails.jswaterline

解决方案


UsageError当我们以不正确的方式使用 db 方法时,Sails 会抛出异常。更准确地说,这就是 Sails 在其文档中的内容。

当错误的名称为:“UsageError”时,这表明 Waterline 方法使用不正确,或使用无效选项执行(例如,尝试创建违反模型的高级验证规则之一的新记录。)

https://sailsjs.com/documentation/concepts/models-and-orm/errors#?usage-errors

举一个简单的例子,如果你有一个带有限制参数的数据库查询,并且你开始为该参数传递字符串值,那么 Sails 就会开始抛出UsageError

在您的情况下,从错误细节本身来看,事情应该是不言自明的。您的代码流在尝试运行时正在undefined运行,但如果您看到.inputs.idLaboratory.archive({id: inputs.id})idconfig/models.js

只需进行基本调试以查看为什么inputs.id未定义并解决该问题,您应该一切顺利。


推荐阅读