2013-02-28 35 views
-1

請告訴我,如何在客戶端上使用骨幹應用程序創建REST API,並在服務器端使用nodejs + mongodb創建REST API。獲取nodejs和mongodb中的收集數據

我是nodejs中的新手,無法理解所有基本的東西。

舉個例子 - 我需要找朋友收藏。 在我的客戶端應用程序我說

var collection = new Backbone.Collection.exted({ 
    model: model, 
    url: '/api/friends' 
}); 
collection.fetch(); 

好了,在服務器上(+的NodeJS快遞)我可以 app.get( '/ API /朋友',friends.get)聽此請求;

在«朋友»模塊我有«get»功能。它連接到數據庫,嘗試從«朋友»集合中獲取數據(如果集合不存在,則必須創建)。如果收集是空的,函數必須初始化請求到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中的教程。

感謝您的建議和對不起,請爲我的英語。

+0

您需要了解異步函數必須與回調一起使用,您無法獲取它們的返回值。 'fetchFriends()。forEach'不能用於實例。 – Greg 2013-02-28 14:53:47

回答

2

有很多代碼在這裏不起作用。以此爲例:

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); 
    }); 
}; 

你不能只叫res.send調用client.colleciton後,你需要的工作獲取getAllFriends完成後,要做到這一點的回調。

對於你在做什麼的異步代碼有一般的理解,我認爲現在不用擔心節點/ mongo教程,而是先更多地使用節點,然後看看異步庫和承諾直到你熟悉異步。

1

我不知道這段代碼應該做什麼。

  • 減少您正在調試的代碼量;
  • 明確指出什麼不能給你你所期望的;
  • 使用控制檯。登錄

你可能會首先找到:

exports.get = function (req, res) { 
    client.open(function (err, pClient) { 
    client.collection('friends', getAllFriends); 
    res.send(friends); 
    }); 
}; 

不起作用,因爲變量「朋友」是不是你希望它是什麼。

+0

好的,謝謝。問題與異步解決)現在我有一個與heroku部署的問題) http://stackoverflow.com/questions/15254327/https-request-from-heroku – user1666362 2013-03-06 19:03:19