javascript - Mongoose query hangs after callback -
this must user error, have reduced example far can, , can't spot it.
here make simple query, calls callback result , no error. however, program hangs afterwards; never exits.
import mongoose "mongoose"; const someschema = new mongoose.schema({ name: string }); const m = mongoose.model("someschema", someschema); mongoose.connect("mongodb://localhost", err => { console.error("connection error", err); m.find({}, (queryerror, result) => console.log("result:", result, "error:", queryerror)); });
result:
connection error undefined result: [ { _id: 55ffada74fb9958d13ec7c4b, name: 'asdf', __v: 0 } ] error: null
(program hangs instead of exiting)
i'm sure i've overlooked basic, can't spot in guide, , juggling things around isn't helping. weird, have bunch of more complex mongoose models working elsewhere.
- mongoose: ^4.0.5
- babel-node: 5.8.20
your program stay open until close mongodb connection (however, not mean "hangs"). put mongoose.disconnect();
when you're finished , script quit.
mongoose.connect("mongodb://localhost", err => { console.error("connection error", err); // todo better handle error m.find({}, (queryerror, result) => { console.log("result:", result, "error:", queryerror); mongoose.disconnect(); }); });
Comments
Post a Comment