gpt4 book ai didi

node.js - 环回: How to access a model from a script?

转载 作者:太空宇宙 更新时间:2023-11-03 23:20:54 26 4
gpt4 key购买 nike

我正在使用 Loopback,并希望通过脚本将数据保存到数据库。

我编写了一个自定义命令,我将通过 cronjob 运行该命令:

'use strict';

var loopback = require('loopback');
var app = module.exports = loopback();
var boot = require('loopback-boot');

app.start = function() {
return app.listen(function() {
const baseUrl = app.get('url').replace(/\/$/, '');
console.log('Web server listening at: %s', baseUrl);
let Dish = app.models.dish;
console.log(Dish);
})
}

boot(app, __dirname, function(err) {
if (err) throw err;

// start the server if `$ node server.js`
if (require.main === module)
app.start();
});

我得到的输出是:

Web server listening at: http://localhost:3000
undefined

如何访问碟子模型?

最佳答案

您没有调用启动函数

https://github.com/strongloop/loopback-boot

The loopback-boot module initializes (bootstraps) a LoopBackapplication. Specifically, it:

Configures data-sources.

Defines custom models Configures models and attaches models to data-sources.

Configures application settings

Runs additional boot scripts, so you can put custom setup code in multiple small files instead of in the main application file.

您的服务器 js 可能包含与此类似的内容

var boot = require('loopback-boot');

app.start = function() {
return app.listen(function() {
const baseUrl = app.get('url').replace(/\/$/, '');
console.log('Web server listening at: %s', baseUrl);
if (app.get('loopback-component-explorer')) {
const explorerPath = app.get('loopback-component-explorer').mountPath;
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
}
})
}

boot(app, __dirname, function(err) {
if (err) throw err;

// start the server if `$ node server.js`
if (require.main === module)
app.start();
});

您需要这些来初始化应用程序。您也许可以只调用 boot 来逃脱,但我认为 app.start 是连接数据源的那个。

关于node.js - 环回: How to access a model from a script?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50022593/

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