0
var cradle = require('cradle');
var db = new(cradle.Connection)().database('starwars');

db.get('vader', function (err, doc) {
    doc.name;
    assert.equal(doc.force, 'dark');
});

db.save('skywalker', {
    force: 'light',
    name: 'Luke Skywalker'
}, function (err, res) {
    if (err) {
        // Handle error
    } else {
        // Handle success
    }
});

通过 npm 安装的摇篮

npm install cradle

错误输出运行node cradle.js

root@server:~# node cradle.js

/root/cradle.js:5
    doc.name;
       ^
TypeError: Cannot read property 'name' of undefined
    at Object.callback (/root/cradle.js:5:8)
    at /root/node_modules/cradle/lib/cradle.js:276:26
    at IncomingMessage.<anonymous> (/root/node_modules/cradle/lib/cradle.js:210:21)
    at IncomingMessage.emit (events.js:81:20)
    at HTTPParser.onMessageComplete (http.js:133:23)
    at Socket.ondata (http.js:1213:22)
    at Socket._onReadable (net.js:681:27)
    at IOWatcher.onReadable [as callback] (net.js:177:10)

更新:问题归结为

db.get('vader', function (err, doc) {
        doc.name;
        assert.equal(doc.force, 'dark');
    });

它在抱怨doc.name

我在哪里可以查看数据库以便我可以看到添加的新行?

4

1 回答 1

1
db.get('vader', function (err, doc) {
    if (err) {
        console.log(err);
    } else {
        doc.name;
        assert.equal(doc.force, 'dark');
    }
});

该示例不进行错误处理。我会假设错误是文档vader不存在或数据库starwars不存在。

如果您已安装并运行沙发,则数据库位于http://localhost:5984/_utils/

于 2011-06-12T17:36:00.637 回答