为什么不在回调中工作函数?
Why don't work functions in callback?
第一个函数findOne工作正常。但是这里所有的Mongoose函数回调都不能正常工作,没有任何错误。为什么?谢谢你!
var mongoose = require('mongoose');
var Links = require('../models/Links');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/soft');
Links.findOne({}, function(err, l) {
if (err) throw err;
console.log("1", l);
Links.findOne({}, function(err, l_) {
if (err) throw err;
console.log("2", l_);
});
});
mongoose.connection.close();
代码在第二个.find
有机会被调用之前关闭了连接。由于.find
是异步的,代码执行第一个调用(它有一个连接来执行),但随后继续并在第一个调用返回执行第二个调用之前断开连接。
将.close
调用移到第二个调用中,将允许两个呼叫都进行。
var Links = require('../models/Links');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/soft');
Links.findOne({}, function(err, l) {
if (err) throw err;
console.log("1", l);
Links.findOne({}, function(err, l_) {
if (err) throw err;
console.log("2", l_);
mongoose.connection.close();
});
});
相关文章:
- onmouseout函数工作不正常
- 我无法使onmouseout函数工作
- Javascript”;结束”;函数工作不稳定
- HTML JavaScript,我如何能够通过给每个元素一个不同的Id来使用JavaScript使这个函数工作
- Javascript:onclick函数工作一次,页面就会恢复到原来的状态
- jQuery toggle()函数工作不正常
- Node.js npm opn-can't使回调函数工作
- 添加两个数字的JavaScript函数工作不正常
- 调用在Jquery中未作为链接函数工作
- 将PHP变量传递给Javascript不能通过PHP函数工作(与全局变量无关)
- 为什么我需要在 javascript 函数工作之前按提交按钮 2 次
- 使 JavaScript 函数工作时遇到麻烦
- 如何使 clearInterval 函数工作
- 如何使 then 函数工作
- 如何让我的包含函数工作
- Promise.all 不应该作为传递给 .then 的函数工作
- 我无法让它作为 JavaScript 函数工作(我在控制台时得到我想要的返回值.log但我不能将其作为函数调用)
- JavaScript on change 函数工作得不好
- jquery next()函数工作不正常
- Jquery hasClass()函数工作不正常