gpt4 book ai didi

javascript - ID : null issue in sailsJS when create an User

转载 作者:太空宇宙 更新时间:2023-11-04 00:55:54 24 4
gpt4 key购买 nike

我怀疑我的问题有些愚蠢,但我找不到错误。我正在使用 mongodb 来保存用户数据。每个似乎都工作正常,但是当我查看数据库时,每个用户记录中都有 id: null 。我不需要 id: null,我已经有 _id 字段。

$sails --version
0.11.0

我的用户 Controller :

module.exports = {
create: function(req, res) {
User.create(req.params.all(), function userCreated(err, user) {
if(err) res.json(401, err);
res.json(200, user);
})
}
}

我的用户模型:

module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
email: {
type: 'string',
email: true,
required: true,
unique: true
},
password: {
type: 'string',
minLength: 6,
maxLength: 15,
columnName: 'encrypted_password',
required: true
},
toJSON: function() {
var obj = this.toObject();
delete obj.password;
return obj;
}
},
beforeCreate: function(values, next) {
require('bcrypt').hash(values.password, 10, function passwordEncrypted(err, encryptedPassword) {
if(err) console.log(err);
values.password = encryptedPassword;
next();
});
}
};

当我从像http://localhost:1337/user/create?name=theUser&email=user@mail.com&password=123456&role=admin这样的url创建用户时,一切似乎都很好,但在我的mongodb中我看到了这个:(id: null)

{
name: "theUser",
email: "user@mail.com",
role: "admin",
id: null,
createdAt: ISODate("2015-04-27T18:34:42.678Z"),
updatedAt: ISODate("2015-04-27T18:34:42.678Z"),
encrypted_password: "$2a$10$iNt/OR8XhjijqRjkpoNW/eR70HTSDgVJ2WmNppqab79rZt213aywm",
_id: ObjectId("553e81429255e51f419a8ffc")
}

我尝试使用 autoPK: false 但没有任何反应。提前致谢。

最佳答案

waterline docs 中所述,

By default an attribute named id will be automatically added to your model which will contain an auto-incrementing number unique to each record. This will be your model's primary key and will be indexed when available. You can override this if you would like to define your own primary key factory or attribute.

主键

将设置记录的主键。当 autoPK 设置为 false 时应使用此选项。

attributes: {
uuid: {
type: 'string',
primaryKey: true,
required: true
}
}

关于javascript - ID : null issue in sailsJS when create an User,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29903815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com