Sails-mongo适配器。来自数据库适配器的意外错误。Invalid primary key value provided for `id`.无法将`94...`解释为Mongo id。不能将 "94... "解释为Mongo id。

问题描述 投票:1回答:1

我的sails应用在连接到mysql数据库时工作良好。然而,我需要切换到mongoDB,因为这是项目的要求,我面临的问题就在这里。

我的模型有一些关系,它的工作,为了实现这一点,我不得不做一些修改,其中包括以下内容。我使用了 uuid 来设置每个模型的主键(id)(id是之前由mysql自动生成的)。然而当我尝试向服务器提交请求时,我遇到了错误。

AdapterError: Unexpected error from database adapter: Invalid primary key value provided for `id`.  Cannot interpret `9456b206-ebcf-4a6d-b81c-93964c027f04` as a Mongo id.
(Usually, this is the result of a bug in application logic.)

这是我的一个模型的例子--pick.js。

module.exports = {
  
  attributes: {
    userId: {
      type: 'string',
      required: true,
      //unique: true,
    },
    comment:{
      type: 'string',
      required: true
    },
    createdBy:{
      type: 'string',
      required: true
    },
    info: {
      model: 'personalinfo' 
    },
    admin: {
      model: 'admin'
    },

    id:{
      type: 'string',
    },

  },

};
node.js mongodb sails.js
1个回答
0
投票

我找到了一个简单的方法来解决这个问题。我没有使用uuid来设置主键。Sails已经在config.models.js中使用下面的代码段来连接mongo db并方便地设置主键。

attributes: {
  ...
  //id: { type: 'number', autoIncrement: true, },
  id: { type: 'string', columnName: '_id' },
  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.