我想确保我以前的函数必须在Nodejs服务器端脚本执行以下代码之前完全执行
I want to make sure that my previous function must be executed completely before the following code in Nodejs server side scripting
我想确保我的mongodb在mongoConnect函数中成功连接,在我开始查询之前,因为在NodeJS中是非阻塞的,它继续执行语句
我只是想知道是否有一些函数像"。then(function(){..})"在服务器端或任何其他类似的东西,我知道有回调函数用于目的,但我不知道如何使用它在这里,我的意思是在我的情况下。
这是我的代码:
io.sockets.on('connection', function (socket) {
mongoConnect();
//Some Query to the Database
socket.on('login',function(user)
{
controller.loginUser(user);
});
});
function mongoConnect()
{
var mongoose = require('mongoose');
mongoose.connect('mongodb://xxxxxx-xxx-xxx.mongolab.com:xxx/xxxx');
}
您可以使用猫鼬。一次等待'open'事件,然后执行回调。
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.once('open', function callback () {
// Stuff here
});
参考这个网站:http://mongoosejs.com/docs/index.html
相关文章:
- 如果返回的数据是javascript,$.ajax会自动执行脚本吗
- 如果条件匹配,则停止执行脚本
- 加载服务器端渲染的React组件后执行脚本
- 量角器使用ElementArrayFinder执行脚本怪异
- 在selenium python中执行脚本
- 仅当脚本在视口中时执行脚本
- Chrome扩展:加载窗口后执行脚本
- FireFox扩展对TinyMCE编辑器执行脚本注入
- HTML表单-按'时执行脚本;输入',无需提交
- 当超出范围时,延迟执行脚本Jquery(或SetTimeOut)BUT
- 在执行脚本之前,请查看对象是否未定义
- 如何在整个网页加载后执行脚本
- 多次执行脚本的 jQuery
- 从另一个文件执行脚本
- 从单独的.php文件中执行<脚本>
- 如何防止执行脚本但在 DOM 中显示它
- 在 Mac 上的 Firebug 中继续执行脚本的快捷方式是什么?
- 文档.写入脚本内部不执行脚本
- 在页面加载之前在 ajax 请求的页面上执行脚本
- 如何对包含在父 html 中的子 html 文件执行脚本