[英]find not working on mongoose document
我的模特看起來像這樣
/* models/songs.js */
var mongoose = require('mongoose');
var SongsSchema = new mongoose.Schema({
genres: [ String ],
popularity: Number,
trackId: String,
artistIds: [ String ],
users: [ String ],
trackName: String // human readability
});
var Songs = mongoose.model("Songs", SongsSchema);
module.exports = {
Songs: Songs
};
這就是我的路線
/* routes/index.js */
var mongoose = require('mongoose');
var Songs = mongoose.model('Songs');
router.get('/api/genre', function(req, res, next) {
var genre = req.query.genre.replace(/%26/, '&');
genre = req.query.genre.replace(/-/g, ' ');
console.log('attempting to find', genre);
Songs.find({
genres: genre
}, function(err, songs, count) {
console.log('count', count);
console.log('songs', songs);
res.json(songs.map(function(song) {
console.log('hello ', song);
return song.trackId;
}));
});
});
“嘗試查找”可以很好地注銷,但是Songs.find回調中的任何一行都不會注銷任何內容。 奇怪的是,這沒有給我任何錯誤。 另一個奇怪的事情是,我還嘗試在routes/index.js
其他部分中使用Songs.find,它們工作正常。 我也嘗試在Songs文檔中使用findOne
和其他功能,但它們似乎無法正常工作。 我也嘗試查看其他StackOverflow問題,但似乎沒有一個答案。
find
Query中的callback
有2個參數, first
一個是error
, second
個是array of mathced documents
。
在查詢中,您提供了3個參數。 將其更改為2,看看是否可行。我想,它將正常工作。
為了count
你可以做songs.length
做這個:
Songs.find({
genres: genre
}, function(err, songs) {
console.log('count', songs.length);
console.log('songs', songs);
res.json(songs.map(function(song) {
console.log('hello ', song);
return song.trackId;
}));
});
嘗試這個
Songs.find({
genres: genre
}).exec(function(err, songs) {
console.log('songs', songs);
res.json(songs.map(function(song) {
console.log('hello ', song);
return song.trackId;
}));
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.