我使用 sails js 0.12.14 和 mongodb。如果我在插入文档后尝试获取文档,则文档为空
Products.create(product).exec(function(err, createdProduct) {
Products.find({_id : createdProduct.id}).exec(function(err, foundProductAfterCreate) {
console.log(foundProductAfterCreate);
})});
有人可以解释为什么该文档不可用吗?
更新: 这对我来说是正确的代码...
Products.create(product).exec(function(err, createdProduct) {
Products.find({_id : createdProduct[0].id}).exec(function(err, foundProductAfterCreate) {
console.log(foundProductAfterCreate);
})});
最佳答案
您正在查询的文档实际上与您已有的文档相同
Products.create(product).exec(function(err, createdProduct) {
// handle the error...
console.log(createdProduct); // the full record
});
createdProduct
与通过 id 查询时得到的对象完全相同。
如果您确实需要通过 id 进行查询,sails 会从 mongo 标准的 _id
到 id
进行相当全面的切换没有下划线。你会这样做:
Products.findOne({id: createdProduct[0].id}).exec(function(err, foundAgain) { // etc
...除非您使用 .native
进行更基本的 mongo 查询访问,否则任何地方都没有下划线。
关于javascript - Mongo在插入后查找文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47535699/