我有一个基本脚本来查找没有名称字段的所有客户,并记录它们。返回错误Customer.find is not a function
。其余的代码在正常运行应用程序时运行正常,因此它不是模块导出问题。正常运行应用程序时,客户模块可以正常运行。我通过调用node server/scripts/script.js
有什么想法吗?完整代码和错误如下。
脚本
'use strict';
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
require('babel-core/register');
const mongoose = require('mongoose');
const config = require('../config/environment');
const Customer = require('../api/customer/customer.model');
mongoose.connect(config.mongo.uri, config.mongo.options);
console.log('Customer');
console.log(Customer);
Customer.find({
$or: [
{ 'name': false },
{ 'name': '' },
],
}, function (err, customers) {
if (err) return console.error(err);
console.log(customers.length);
});
错误
Customer
{ default:
{ [Function: model]
hooks: Kareem { _pres: {}, _posts: {} },
base:
Mongoose {
connections: [Object],
plugins: [],
models: [Object],
modelSchemas: [Object],
options: [Object] },
modelName: 'Customer',
model: [Function: model],
db:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'toro-dev',
options: [Object],
otherDbs: [],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
_listening: false,
db: [Object] },
discriminators: undefined,
findScript: [Function: findScript],
schema:
Schema {
paths: [Object],
subpaths: {},
virtuals: [Object],
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [Object],
_indexes: [],
methods: [Object],
statics: [Object],
tree: [Object],
_requiredpaths: undefined,
discriminatorMapping: undefined,
_indexedpaths: undefined,
query: {},
childSchemas: [Object],
s: [Object],
options: [Object],
'$globalPluginsApplied': true },
collection:
NativeCollection {
collection: null,
opts: [Object],
name: 'customers',
collectionName: 'customers',
conn: [Object],
queue: [],
buffer: true,
emitter: [Object] },
Query: { [Function] base: [Object] },
'$__insertMany': [Function],
insertMany: [Function] } }
Customer.find({
^
TypeError: Customer.find is not a function
at Object.<anonymous> (/Users/tori/Documents/RocketSled/toro/server/scripts/remove_empty_customers.js:16:10)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
修改
添加了上面Customer
的控制台日志,以便您可以看到它与
答案 0 :(得分:0)
问题是babel - 在我的程序的其余部分,我实施了babel,以便我的Customer模型可以以某种方式导出。然而,使用一次性脚本,babel没有像往常一样集成或需要,所以我必须按照旧学校的ES5语法导出Customer模型。将我的客户导出更改为:
export default mongoose.model('Customer', CustomerSchema);
module.exports = mongoose.model('Customer', CustomerSchema);
现在效果很好。在我的典型代码中,我只使用第一行。我希望这有助于其他人!