【发布时间】:2013-02-20 00:14:07
【问题描述】:
所以我遇到了这个让我在过去 4 天里一直很忙的问题,我有一个这样的架构和子文档架构:
var mongoose = require( 'mongoose' ),
Schema = mongoose.Schema;
var ProjectSchema = new Schema({
name: String,
author: String,
category: String,
description: String,
tags: [TagSchema]
});
var TagSchema = new Schema({
name: String,
date: Date
});
mongoose.model( 'TagSchema', TagSchema );
mongoose.model( 'Project', Project );
我想要的是所有 ProjectSchemas 的所有标签的列表,无论我尝试什么,我要么得到 NONE,要么只是最新项目的标签。我只是不知道更多,因为无论我做什么,我总是以失败告终。我做错了什么,猫鼬没有 findAll 这样的东西吗?
app.get('/tags.json', function(req, res) {
TagSchema.find({}, function ( err, tags ){
var json = JSON.stringify(tags);
console.log(json);
tags = tags.map(function (tag) {
return tag.toObject();
});
console.log(json == JSON.stringify(tags));
res.json(json);
});
});
和
app.get('/tags.json', function(req, res) {
Project.find(function (err, projects) {
projects.forEach( function( project ){
res.json(project.tags);
});
});
});
我尝试过的其他任何东西都返回了
[ ]
或者出错了……
(另外我想知道,如果我将标签添加到项目中并且它已经存在,我如何才能确保它不会添加。)
【问题讨论】:
标签: json node.js express mongoose