简体   繁体   English

无法获得默认设置以使用水线/帆

[英]Can't get defaultsTo to work with waterline/sailsjs

I'm trying to set a default value to a field when I create a new item in the database, but I can't make it to work. 我试图在数据库中创建新项目时将默认值设置为字段,但无法使其正常工作。

I am using SailsJS 0.12.1 currently, which is the latest version 我目前正在使用SailsJS 0.12.1,这是最新版本

This is how my file looks like: 这是我的文件的样子:

models/Users.js models / Users.js

module.exports = {

    schema: true,

    attributes: {
        firstName: {
            type: 'string',
            required: true,
        },
        role: {
            type: 'string',
            enum: ['admin', 'user', 'limited', 'suspended', 'deleted'],
            defaultsTo: 'limited'
        },
    }
}

When the element is created in the database, I don't get the role value. 在数据库中创建元素时,我没有获得role值。 It looks like that: 看起来像这样:

{
    "_id": {
        "$oid": "56f8616163a59cf813998f8a"
    },
    "firstName": "Alex",
    "createdAt": {
        "$date": "2016-03-27T22:40:33.151Z"
    },
    "updatedAt": {
        "$date": "2016-03-27T22:40:33.151Z"
    }
}

Any idea what I forgot? 知道我忘记了什么吗?

Thanks! 谢谢!

I had a similar issue earlier today. 我今天早些时候遇到过类似的问题。 I was able to resolve this by adding required is 'true'. 我能够通过添加required为'true'来解决此问题。

Example of New User Model: 新用户模型示例:

 module.exports = {

    schema: true,

    attributes: {
        firstName: {
            type: 'string',
            required: true,
        },
        role: {
            type: 'string',
            required: true,
            enum: ['admin', 'user', 'limited', 'suspended', 'deleted'],
            defaultsTo: 'limited'
        },
    }
}

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM