简体   繁体   English

在Node.js中建立模型的正确方法

[英]Correct way to set up models in Node.js

I'm building a small Node/Express app and I have set up Knex with a postgresql adapter. 我正在构建一个小型Node / Express应用程序,并且已使用postgresql适配器设置了Knex。 That all works. 一切正常。 I know it works because I can do a sql query and the results are what I want - the rows from a particular table. 我知道它的工作原理是因为我可以执行sql查询,而结果就是我想要的-特定表中的行。 But I'm trying to set up models so that I can bring in related data and do validations. 但是我正在尝试建立模型,以便可以引入相关数据并进行验证。 I'm using bookshelf for this. 我为此使用书架。

I have set up my models like this: 我已经建立了这样的模型:

base.js base.js

var knex = require('../../config/db');
var bookshelf = require('bookshelf')(knex);
bookshelf.plugin('registry');

module.exports = bookshelf;

food.js food.js

var bookshelf = require('./base');
var Meal = require('./meal');

var Food = bookshelf.Model.extend({
  tableName: 'foods',
  meal: function() {
    return this.belongsTo(Meal);
  }
});

module.exports = bookshelf.model('Food', Food);

meal.js meal.js

var bookshelf = require('./base');
var Day = require('./day');
var Food = require('./food');

var Meal = bookshelf.Model.extend({
  tableName: 'meals',
  foods: function() {
    return this.hasMany(Food);
  },
  day: function() {
    return this.belongsTo(Day);
  }
});

module.exports = bookshelf.model('Meal', Meal);

day.js day.js

var bookshelf = require('./base');
var Meal = require('./meal');

var Day = bookshelf.Model.extend({
  tableName: 'days',
  meals: function() {
    return this.hasMany(Meal);
  }
});

module.exports = bookshelf.model('Day', Day);

The problem is that when I require the model in the router, I get a NotFoundError like this: 问题是,当我在路由器中需要模型时,会收到一个NotFoundError如下所示:

model: 
  { [Function]
    NotFoundError: [Function: ErrorCtor],
    NoRowsUpdatedError: [Function: ErrorCtor],
    NoRowsDeletedError: [Function: ErrorCtor] }

I'm trying to retrieve the db results like this: 我正在尝试检索这样的数据库结果:

var Meal = require('../models/meal');

var meals = Meal.fetchAll().then(function(collection) {
  return collection;
});
...

Why is this not returning anything? 为什么这不返回任何东西? This looks like what the documentation suggests. 这看起来像文档中所建议的。

Where am I going wrong? 我要去哪里错了? I'm guessing it has to do with not setting up bookshelf right or requiring the models correctly. 我猜想这与没有正确设置书架或正确要求模型有关。

Any advice? 有什么建议吗?

One reason that this call to fetchAll is not returning anything may be because the database query is asynchronous. 对该fetchAll的调用未返回任何内容的一个原因可能是因为数据库查询是异步的。 If you are trying to use the variable meals later on in your code, it will likely still be undefined. 如果您稍后尝试在代码中使用可变餐食,则可能仍未定义。 You should put the rest of your code that needs this collection inside the .then callback, or in a new .then callback so that it will run with access to the queried data. 您应该将需要此集合的其余代码放在.then回调中,或放入新的.then回调中,以便它可以访问查询的数据来运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM