gpt4 book ai didi

node.js - 初始化独立 Waterline 后无法查询集合

转载 作者:太空宇宙 更新时间:2023-11-04 02:28:37 24 4
gpt4 key购买 nike

我在我的express.js应用程序中使用独立的Waterline(带有sails-disk驱动器)。

我收到以下错误:

/home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/lib/waterline/core/index.js:70
var schemaAttributes = this.waterline.schema[this.identity].attributes;
^
TypeError: Cannot read property 'schema' of undefined
at Core._initialize (/home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/lib/waterline/core/index.js:70:40)
at module.exports (/home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/lib/waterline/core/index.js:50:20)
at module.exports (/home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/lib/waterline/collection/index.js:44:8)
at child (/home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/lib/waterline/utils/extend.js:17:39)
at /home/tux/IdeaProjects/my-first-waterline-thing/bin/www.js:12:47
at /home/tux/IdeaProjects/my-first-waterline-thing/database.js:36:9
at /home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/lib/waterline.js:214:7
at /home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/node_modules/async/lib/async.js:157:25
at /home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/lib/waterline/query/index.js:70:7
at /home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/lib/waterline/adapter/sync/strategies/alter.js:190:22
at bound (/home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/node_modules/lodash/dist/lodash.js:957:21)
at applyInOriginalCtx (/home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/lib/waterline/utils/normalize.js:416:80)
at wrappedCallback (/home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/lib/waterline/utils/normalize.js:315:18)
at _normalizeCallback.callback.success (/home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31)
at _switch (/home/tux/IdeaProjects/my-first-waterline-thing/node_modules/waterline/node_modules/switchback/lib/factory.js:48:28)
at null._onTimeout (/home/tux/IdeaProjects/my-first-waterline-thing/node_modules/sails-disk/lib/adapter.js:179:10)

这是我用来测试 Waterline 使用情况的代码(在我的 bin/www 文件中):

require('../database.js')(app, function(err) {
if (err) {
return console.log("Waterline didn't initialize: " + err);
}
// Smoke-test to verify Waterline functionality
var testModel = require('../models/users')();
testModel.find({willNotExist: 42}, function(err, cb) {
if (!err) {
console.log('Waterline seems to work');
var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + server.address().port);
});
} else {
console.log(err);
}
});
});

我的database.js看起来像这样:

var Waterline = require('waterline');
var User = require('./models/users.js');
var sailsDisk = require('sails-disk');

module.exports = function(app, cb) {
var config = {
adapters: {
'default': sailsDisk
},
connections: {
'default': {
adapter: 'default'
}
},
defaults: {
migrate: 'alter'
}

};

var orm = new Waterline();
orm.loadCollection(User);

orm.initialize(config, function(err, models) {
app.collections = models.collections;
cb(err);
});
};

最佳答案

我认为您应该在初始化之前加载集合,就像在database.js中一样,因此您可以使用app.collections.user.find(...)直接访问它们,而不是在testModel中再次加载它。

关于node.js - 初始化独立 Waterline 后无法查询集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28707764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com