本文介绍了为什么node.js方法的findRandom()猫鼬不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
模式:
var random = require('mongoose-random');
var productSchema = new mongoose.Schema(...)
productSchema.plugin(random, {path: 'r'});
var products = mongoose.model('sportsStore', productSchema);
module.exports = products;
调用findRandom(),控制台显示"[]"
Calling findRandom(), console displays "[]"
getRandomProducts: function (count) {
products.findRandom().limit(count).exec(function(error, result) {
console.log(result); // -> []
})
方法find()正常工作.
Method find() works correctly.
推荐答案
可能的解决方案在这里-请参见 npm页面猫鼬随机示例.
And probably solution could be here - see the comments on npm page mongoose-random example .
// if you have an existing collection, it must first by synced.
// this will add random data for the `path` key for each doc.
尝试
productSchema.syncRandom(function (err, result) {
console.log(result.updated);
});
这篇关于为什么node.js方法的findRandom()猫鼬不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!