Node.js 运行脚本以修改数据库-Mongoose Model.find不是一个函数
我有一个基本的脚本来查找所有没有名称字段的客户,并记录他们。返回的错误Node.js 运行脚本以修改数据库-Mongoose Model.find不是一个函数,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我有一个基本的脚本来查找所有没有名称字段的客户,并记录他们。返回的错误Customer.find不是函数。在正常运行应用程序时,其余代码工作正常,因此这不是模块导出问题。正常运行应用程序时,客户模块工作正常。我通过调用节点服务器/scripts/script.js执行此脚本 有什么想法吗?下面是完整的代码和错误 脚本 'use strict'; process.env.NODE_ENV = process.env.NODE_ENV || 'development'; require('bab
Customer.find不是函数。在正常运行应用程序时,其余代码工作正常,因此这不是模块导出问题。正常运行应用程序时,客户模块工作正常。我通过调用节点服务器/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
客户
{默认值:
{[功能:模型]
hooks:Kareem{{u press:{},{u posts:{},
基数:
猫鼬{
连接:[对象],
插件:[],
模型:[对象],
modelSchemas:[对象],
选项:[对象]},
modelName:'客户',
型号:[功能:型号],
db:
国家联系{
base:[对象],
集合:[对象],
模型:[对象],
配置:[对象],
复制品:假,
主机:空,
主机:“localhost”,
港口:27017,
用户:未定义,
通过:未定义,
名称:'toro dev',
选项:[对象],
其他数据库:[],
_重新编制日期:2,
_这是错误的,
_他说:错,
_听:错,
db:[对象]},
鉴别器:未定义,
findScript:[函数:findScript],
模式:
模式{
路径:[对象],
子路径:{},
虚拟对象:[对象],
SingleNestedPath:{},
嵌套:{},
继承:{},
调用队列:[对象],
_索引:[],
方法:[对象],
静态:[对象],
树:[对象],
_所需路径:未定义,
应用程序:未定义,
_IndexedPath:未定义,
查询:{},
childSchemas:[对象],
s:[对象],
选项:[对象],
“$globalPluginsApplied”:true},
收藏:
本地采集{
集合:null,
选项:[对象],
名称:'客户',
collectionName:'客户',
conn:[对象],
队列:[],
缓冲区:对,
发射器:[对象]},
查询:{[Function]base:[Object]},
“$\uu insertMany”:[函数],
insertMany:[函数]}
客户。查找({
^
TypeError:Customer.find不是一个函数
在对象上。(/Users/tori/Documents/RocketSled/toro/server/scripts/remove\u empty\u customers.js:16:10)
在模块处编译(Module.js:409:26)
在Object.Module.\u extensions..js(Module.js:416:10)
在Module.load(Module.js:343:32)
在Function.Module.\u加载(Module.js:300:12)
位于Function.Module.runMain(Module.js:441:10)
启动时(node.js:139:18)
在node.js:968:3
编辑:
在上面添加了客户的控制台日志
,以便您可以看到它实际链接到的内容问题是babel-在我的程序的其余部分,我实现了babel,以便可以以某种方式导出我的客户模型。但是,使用一次性脚本时,babel没有像往常一样集成或需要,因此我必须导出客户模式l按照老派ES5语法。将我的客户导出更改为:
export default mongoose.model('Customer', CustomerSchema);
module.exports = mongoose.model('Customer', CustomerSchema);
现在它工作得很好。在我的典型代码中,我只使用了第一行。我希望这对其他人有帮助!问题是babel-在我程序的其余部分,我实现了babel,以便可以以某种方式导出我的客户模型。但是,使用一次性脚本时,babel没有像往常一样集成或需要,因此我必须导出C符合旧式ES5语法的客户模型。将我的客户导出更改为:
export default mongoose.model('Customer', CustomerSchema);
module.exports = mongoose.model('Customer', CustomerSchema);
现在它工作得很好。在我的典型代码中,我只使用第一行。我希望这对其他人有帮助