Javascript 无法在compose.io上的Sails.js中获取ID
我正在创建一篇博文,而自动创建的ID从未进入compose.io上的MongoDB数据库 下面是我的beforeCreate函数的json结果(仅用于测试) 以下是我的BlogController中创建函数的json结果:Javascript 无法在compose.io上的Sails.js中获取ID,javascript,mongodb,sails.js,Javascript,Mongodb,Sails.js,我正在创建一篇博文,而自动创建的ID从未进入compose.io上的MongoDB数据库 下面是我的beforeCreate函数的json结果(仅用于测试) 以下是我的BlogController中创建函数的json结果: { free: true, published: true, title: 'hiurwgier', slug: 'hiurwgier', post: 'bfjirwbfejkrf', id: '55887edba5bb4113e4173edd', c
{ free: true,
published: true,
title: 'hiurwgier',
slug: 'hiurwgier',
post: 'bfjirwbfejkrf',
id: '55887edba5bb4113e4173edd',
createdAt: Mon Jun 22 2015 16:32:11 GMT-0500 (CDT),
updatedAt: Mon Jun 22 2015 16:32:11 GMT-0500 (CDT) }
create: function(req, res) {
Blog.create(req.params.all(), function newPost (err, post) {
if(err) res.badRequest();
console.log(post);
res.json(post);
});
},
下面是我的compose.io数据库中的json数据存储(注意id上的空值):
以下是我的博客模式:
module.exports = {
attributes: {
free: {
type: 'boolean',
required: true,
defaultsTo: false
},
published: {
type: 'boolean',
required: true,
defaultsTo: false
},
title: {
type: 'string',
required: true
},
slug: {
type: 'string',
required: true
},
post: {
type: 'text',
required: true
}
},
beforeCreate: function(val, next) {
console.log(val);
next();
}
};
这是我在BlogController中的创建函数:
{ free: true,
published: true,
title: 'hiurwgier',
slug: 'hiurwgier',
post: 'bfjirwbfejkrf',
id: '55887edba5bb4113e4173edd',
createdAt: Mon Jun 22 2015 16:32:11 GMT-0500 (CDT),
updatedAt: Mon Jun 22 2015 16:32:11 GMT-0500 (CDT) }
create: function(req, res) {
Blog.create(req.params.all(), function newPost (err, post) {
if(err) res.badRequest();
console.log(post);
res.json(post);
});
},
我没有看到您的模型中定义的
id
属性。您是否尝试过设置schema:false
?感谢@JuanSolano的回复。它被设置为schema:false
,id属性应该会通过Waterline文档自动添加。我已经联系到compose.io,查看是否有任何类型的设置缺少。我在他们的平台上有多个Mongo数据库,但从未遇到过这个问题。这就是为什么我被难住了。那么\u id
属性呢,这是Mongo的默认命名约定。这就是你应该使用的。我以为你的id
是你在做的一些内部事情,但是如果它是你正在寻找的KP,那么\u id
就是其中之一。