[英]get collection data in nodejs and mongodb
請告訴我,我如何使用客戶端上的主干應用程序以及服務器端的nodejs + mongodb創建REST API。
我是nodejs的新手,無法理解所有基本知識。
僅舉例來說,我需要收集朋友。 在我的客戶應用中,我說
var collection = new Backbone.Collection.exted({
model: model,
url: '/api/friends'
});
collection.fetch();
好的,在服務器上(nodejs + express),我可以使用app.get('/ api / friends',friends.get);收聽此請求。
在“朋友”模塊中,我具有“獲取”功能。 它連接到數據庫,嘗試從«friends»集合中獲取數據(如果不存在集合,則必須創建它)。 如果collection為空,則函數必須初始化向vk.com(社交網絡)服務器的數據請求。
/*
* GET friends
*/
var https = require('https'),
Db = require('mongodb').Db,
Server = require('mongodb').Server;
var client = new Db('data', new Server('127.0.0.1', 27017, {})),
friends;
function getAllFriends(err, collection) {
if (!collection.stats()) {
https.get('https://api.vk.com/method/friends.get?access_token=' + global['access_token'], function (d) {
var chunk = '',
response;
d.on('data', function (data) {
chunk += data;
});
d.on('end', function () {
response = JSON.parse(chunk).response;
response.forEach(function (friend) {
collection.insert(friend, function (err, docs) {
if (err) console.log(err);
});
});
}).on('error', function (e) {
console.error(e);
});
});
}
friends = collection.find({}, {
limit: 1000
}).toArray(function (err, docs) {
return docs;
});
}
exports.get = function (req, res) {
client.open(function (err, pClient) {
client.collection('friends', getAllFriends);
res.send(friends);
});
};
但是此代碼不起作用。 我不知道為什么。
/node_modules/mongodb/lib/mongodb/connection/server.js:524
throw err;
^
TypeError: Cannot read property 'noReturn' of undefined
at Cursor.nextObject.commandHandler (/node_modules/mongodb/lib/mongodb/cursor.js:623:17)
at Db._executeQueryCommand (/node_modules/mongodb/lib/mongodb/db.js:1702:5)
at g (events.js:192:14)
at EventEmitter.emit (events.js:126:20)
at Server.Base._callHandler (/node_modules/mongodb/lib/mongodb/connection/base.js:130:25)
at Server.connect.connectionPool.on.server._serverState (/node_modules/mongodb/lib/mongodb/connection/server.js:517:20)
at MongoReply.parseBody (/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:127:5)
at Server.connect.connectionPool.on.server._serverState (/node_modules/mongodb/lib/mongodb/connection/server.js:476:22)
at EventEmitter.emit (events.js:96:17)
at _connect (/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:175:13)
也許這段代碼沒有在需要時創建集合?
也許我做錯了任何事,所以您可以給我一個很好的鏈接,以閱讀nodejs或mongo中的教程。
感謝您的建議,對不起,請您說我的英語。
有很多代碼在這里行不通。 以這個為例:
if (!collection.stats()) {
fetchFriends().forEach(function (friend) {
collection.insert(friend, function (err, docs) {
if (err) console.log(err);
});
});
}
friends = collection.find({}, {
limit: 1000
}).toArray(function (err, docs) {
return docs;
});
}
首先,您無法使用fetchFriends做到這一點。 該函數不返回任何內容,所有工作均在異步回調中完成。
其次,如果它為空,則您仍在觸發直接查找的調用,而不必等到插入結果完成。
甚至呼叫鏈的開頭也壞了:
exports.get = function (req, res) {
client.open(function (err, pClient) {
client.collection('friends', getAllFriends);
res.send(friends);
});
};
您不能只在調用client.colleciton
之后調用res.send
,您需要在getAllFriends中完成工作后在回調中執行此操作。
整個工作過程中普遍缺乏對異步代碼的理解,我認為現在不必擔心節點/ mongo教程,而是先更多地使用節點,然后看看異步庫並保證直到您完成為止。適應異步。
我不知道這段代碼應該做什么。
您可能首先會發現:
exports.get = function (req, res) {
client.open(function (err, pClient) {
client.collection('friends', getAllFriends);
res.send(friends);
});
};
不起作用,因為變量“ friends”不是您期望的那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.