简体   繁体   English

.save()在回调函数中不起作用

[英].save() not working inside callback function

I'm currently using the "request" module to get data from an external page: if i use the following code, it doesn't work: 我目前正在使用“请求”模块从外部页面获取数据:如果我使用以下代码,它将无法正常工作:

request(SITE_URL, function (error, response, body) {
    var user = new gasStation({ id: 12345, name: 'Gustavo' });
    user.save();
});

But if i make the call outside the request function it works as expected: 但是,如果我在request函数之外进行调用,它将按预期工作:

var user = new gasStation({ id: 12345, name: 'Gustavo' });
user.save();
request(SITE_URL, function (error, response, body) {
  // some stuff
});

Why is this happening? 为什么会这样呢?

First never ignore your error handler's. 首先,永远不要忽略您的错误处理程序。 Check if error is true. 检查错误是否为真。 Also your URL might be malformed check that. 另外,您的网址可能格式不正确,请进行检查。 Lastly make sure the mongoose model for user is initialized. 最后,确保用户的猫鼬模型已初始化。 I only saw initialization outside the request. 我只在请求之外看到初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM