0
我有一個從DATEBASE提取數據的應用程序:爲什麼應用程序在使用子字符串方法時掛起?
db.books.find().sort({ _id: 1 }).limit(count).toArray(function (err, results) {
if (err) {
next(err, null);
} else {
next(null, results);
}
});
每本書有長文本的描述,但我不希望得到每本書的全文,所以我決定用map
方法投射的結果,這樣我可以得到字符的數量說明:
db.books.find().sort({ _id: 1 }).limit(count)
.map(function(item) {
return {
description: item.description.substring(0, 50),
,....
}
}).toArray(function (err, results) {
if (err) {
next(err, null);
} else {
next(null, results);
}
});
但是當我使用substring
方法,應用程序掛起,我無法看到的結果。
有什麼想法?
您可以在返回日誌item.description之前添加一條日誌語句,看看它是否得到記錄? – Ewald
嘗試'item.description.slice(0,50),' – Redu
@Ewald感謝它幫助:) –