mongoose查询没有响应
使用mongoose建立了连接以后,通过model查询数据,如:testModel.find()
,既不报错又不返回数据。
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_database');
使用mongoose.connect
连接产生在mongoose.connection
上,使用mongoose.createConnection
是反回一个连接对象,建立model的时候需要使用这个连接才有效,此时使用mongoose.model
是无效的。
const conn = mongoose.createConnection('your connection string');
const MyModel = conn.model('ModelName', schema);
const m = new MyModel;
m.save(); // 可以工作
vs
const conn = mongoose.createConnection('your connection string');
const MyModel = mongoose.model('ModelName', schema);
const m = new MyModel;
m.save(); // 不能工作