【发布时间】:2019-07-22 08:49:21
【问题描述】:
如何从 DAO 获取文档列表并在服务层执行跳过、限制操作?
这是我的 DAO 函数。
function findAllPosts(first,second) {
return Post.find({});
}
这是我的服务层。
function findAllPosts(first, second) {
return new Promises((resolve, reject) => {
postDao.findAllPosts(Number(first), Number(second)).
then((data) => {
var sortingOrd = { 'createdAt': -1 };
resolve(data.sort(sortingOrd).skip(Number(first)).limit(Number(second)));
})
.catch((error) => {
reject(error);
});
});
}
我收到此错误。
TypeError: data.sort(...).skip is not a function
这是模型。
const mongoose = require('mongoose');
var timestamps = require('mongoose-timestamp');
var mexp = require('mongoose-elasticsearch-xp');
var updateIfCurrentPlugin = require('mongoose-update-if-current').updateIfCurrentPlugin;
var PostSchema = new mongoose.Schema({
title: String,
content: String,
categoryId: String,
location: String,
postSummary: String,
postImage: String,
userId: String,
author: String,
urlToImage: String,
newsSrc: String
});
PostSchema.plugin(mexp);
PostSchema.plugin(updateIfCurrentPlugin);
PostSchema.plugin(timestamps);
var Post = mongoose.model('Post', PostSchema);
Post
.esCreateMapping(
{
"analysis": {
"analyzer": {
"my_custom_analyzer": {
"type": "custom",
"tokenizer": "standard",
"char_filter": [
"html_strip"
],
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
}
)
.then(function (mapping) {
// do neat things here
});
Post.on('es-bulk-sent', function () {
});
Post.on('es-bulk-data', function (doc) {
});
Post.on('es-bulk-error', function (err) {
});
Post
.esSynchronize()
.then(function () {
});
module.exports = Post;
出于特定目的,我从 DAO 层删除了排序、跳过和限制。你能告诉我如何在服务层使用这些吗?是否有将“数据”数组转换为 DocumentQuery 对象的显式方法?
【问题讨论】:
-
你的猫鼬模型在哪里?
-
添加了 Post 模型。
-
我不明白你为什么用
data.sort()而不是Post.save()
标签: node.js mongodb mongoose dao service-layer