javascript - 无法在模型 `user` 上找到主键属性。所有模型都必须包含一个充当主键的属性

标签 javascript mongodb express waterline

我有一个如下定义的水线模型,

var Waterline = require('Waterline');
var bcrypt = require('bcrypt');

var User = Waterline.Collection.extend({
identity: 'user',
datastore: 'myMongo',
autoPK: false,
attributes: {
    id: {
        type: 'integer',
        autoIncrement: true,
        primaryKey: true
    },
    email: {
        type: 'string',
        email: true,
        required: true,
        unique: true
    },
    username: {
        type: 'string',
        required: true,
    },

    image: {
        type: 'string'
    },

    password: {
        type: 'string',
        required: true
    },

    mobNo: {
        type: 'string',
        required: true
    },
    aadharNo: {
        type: 'string',
        required: true
    },

    toJSON: function () {
        var obj = this.toObject();
        delete obj.password;
        return obj;
    }
},

beforeCreate: function (values, next) {
    var bcrypt = require('bcrypt');

    bcrypt.genSalt(10, function (err, salt) {
        if (err) return next(err);

        bcrypt.hash(values.password, salt, function (err, hash) {
            if (err) return next(err);

            values.password = hash;
            next();
        });
    });
}
});

module.exports = User;

运行应用程序时,当水线初始化时,我收到以下错误

userError: Could not find a primary key attribute on the model `user`. All models must contain an attribute that acts as the primary key and is guaranteed to be unique.
at normalizeCollection (C:\Users\shriko\WebstormProjects\baclasses\node_modules\waterline-schema\lib\waterline-schema\schema.js:85:44)
at arrayEach (C:\Users\shriko\WebstormProjects\baclasses\node_modules\@sailshq\lodash\lib\index.js:1439:13)
at Function.<anonymous> (C:\Users\shriko\WebstormProjects\baclasses\node_modules\@sailshq\lodash\lib\index.js:3500:13)
at schemaBuilder (C:\Users\shriko\WebstormProjects\baclasses\node_modules\waterline-schema\lib\waterline-schema\schema.js:34:5)
at new WaterlineSchema (C:\Users\shriko\WebstormProjects\balajiclasses\node_modules\waterline-schema\lib\waterline-schema.js:36:12)
at Object.initialize (C:\Users\shriko\WebstormProjects\baclasses\node_modules\waterline\lib\waterline.js:581:26)
at Object.<anonymous> (C:\Users\shriko\WebstormProjects\baclasses\bin\www:32:18)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3 

如您所见,autoPK 为 false,设置了手动主键,仍然得到此信息。

我正在使用“waterline”:“^0.13.1-9”和express。 还剩下什么来创建主键?

谢谢

最佳答案

检查validation Waterline-schema 包中的代码显示主键也必须在架构定义中设置。

var User = Waterline.Collection.extend({
identity: 'user',
datastore: 'myMongo',
autoPK: false,
primaryKey: 'id',
attributes: {
    id: {
        type: 'integer',
        autoIncrement: true,
        primaryKey: true
    }

关于javascript - 无法在模型 `user` 上找到主键属性。所有模型都必须包含一个充当主键的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48353137/

相关文章:

javascript - 无法安装npm?生成应用程序时出现问题

javascript - <音频> 和 javascript : can I force audio files to be redownloaded each time they're used?

javascript - 如何转义字符串中的多个层次引号(HTML)

javascript - Popper.js 不会加载

php - Symfony2 - 数据库级别 onDelete Cascade 不会触发生命周期事件

javascript - mongodb 在简单的查找查询中返回数组大小/计数,而不使用聚合框架

javascript - 当类型不兼容时,如何使 Mongoose 查找查询返回 null 而不是抛出 CastError?

node.js - CoffeeScript 更新未在重新启动时反射(reflect)出来

javascript - Python WebSocket 无法在 Raspberry 上运行

javascript - 使用 Express 和 NodeJS 构建 REST API 的最佳实践