从mongoose数据库获取值时未定义

时间:2022-10-17 09:10:42

For school I have to make a website with angular using MEAN stack. My GET method is returning my spells and comments in json object according to Insomnia.

对于学校,我必须使用MEAN堆栈制作一个有角度的网站。根据Insomnia,我的GET方法在json对象中返回我的法术和注释。

router.get('/API/posts/', function(req, res, next) {
  let query = Post.find().populate("comments").populate("spell");
  query.exec(function(err, posts){
  if(err) { return next(err); }
    res.json(posts);
  });
});

Whenever I try to use my spell I get an undefined. This method gets my items from the get method (I get every value except my spell)

每当我尝试使用我的咒语时,我都会得到一个未定义的。这个方法从get方法获取我的项目(除了我的法术,我得到每个值)

get posts() : Observable<Post[]> {
return this.http.get(`${this._appUrl}/posts/`)
  .pipe(
    map((list: any[]): Post[] =>
      list.map(Post.fromJSON)
  ));
}

My fromJSON method:

我的fromJSON方法:

static fromJSON(json: any): Post {
  const post = new Post(
    json.title,
    json.description,
    json.originalPoster,
    json.category,
    json.spell.map(Spell.fromJSON)
  );
  post._id = json._id;
  post._comments = json.comments;
  return post;
}

Everything worked untill I added the spell to my Post module. I've been searching for a very long time so it would be great if you guys could help me

一切都有效,直到我将这个咒语添加到我的Post模块中。我一直在寻找很长一段时间,所以如果你们能帮助我的话会很棒

My Schema:

我的架构:

var mongoose = require('mongoose');

var PostSchema = new mongoose.Schema({
  title: String,
  description: String,
  spell: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Spell'
  },
  originalPoster: String,
  category: String,
  dateCreated: { type: Date, default: Date.now },
  comments: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Comment'
  }],
  votes: { type: Number, default: 0 }
}); 
mongoose.model('Post', PostSchema);

1 个解决方案

#1


0  

Try populate("comments spell");

尝试填充(“评论拼写”);

#1


0  

Try populate("comments spell");

尝试填充(“评论拼写”);