API mongoDb 中的响应发送问题。
API response send issue in mongoDb.
我正在使用 mongodb 制作后端 API。而且我用过bluebird
来使用promise.
return new promise((resolve, reject) => {
db.collection('employee').find({
email: data.email
}).toArray().then((checkEmail) => {
if (checkEmail.length > 0) {
res.send({ status: 0, message: 'Employee already exist.' });
// I want to stop my node hear.
// I have tried return false , but not works.
}
}).then(() => {
// Add employee details into collection as a new employee.
return db.collection('employee').insert({
//...
})
}).then((employee) => {
// Other stuff
}).catch((error) => {
console.log(error)
res.send({ status: 0, message: 'Something went wrong.' });
});;
}
如您所见,如果 checkEmail > 0
,那么我已经发送了我在邮递员中得到的正确回复。但我的节点仍在执行下一个代码。
那么当我发回 res 时,我怎么能停止下一次执行呢?
我已经将 res 发送给客户端,然后它也执行下一个代码,在其他部分我也发送了 success/error res。这就是我收到此错误的原因。
Error: Can't set headers after they are sent.
我试过用return
、return false
。但它仍然执行我的下一个代码。
不需要在您的 return 语句中创建新的承诺,如果您需要您的方法来产生承诺,您可以 return 链本身。
从 promise 链中的 then
返回不会停止链,它只是将 returned 值作为参数传递给下一个 then
。绕过它的一种方法是抛出您自己的自定义错误并在 catch
中适当地处理它。这样的事情应该有效:
return db
.collection("employee")
.find({email: data.email})
.toArray()
.then(checkEmail => {
if (checkEmail.length > 0) {
let err = new Error("Employee already exists.");
err.code = "EMPLOYEE_ALREADY_EXISTS";
throw err;
}
})
.then(() => {
// Add employee details into collection as a new employee.
return db.collection("employee").insert({
//...
});
})
.then(employee => {
// Other stuff
})
.catch(error => {
if (error.code && error.code === "EMPLOYEE_ALREADY_EXISTS") {
res.send({ status: 0, message: "Employee already exists." });
} else {
console.log(error);
res.send({ status: 0, message: "Something went wrong." });
}
});
编辑:为了再次说明,第三个 then
中的员工将是您之前 then
中的 return,即 db.collection("employee").insert({...})
returns.
你可以像
一样分支你的承诺
db.collection('employee')
.find({
email: data.email
})
.toArray()
.then((checkEmail) => {
if (checkEmail.length > 0) {
return res.send({ status: 0, message: 'Employee already exist.'});
}
else
{
return db.collection('employee').insert({
//...
})
.then((employee) => {
// Other stuff})
})
}
})
.catch((error) => {
console.log(error)
res.send({ status: 0, message: 'Something went wrong.' });
});
或者您可以通过将它与不同的 onSuccess 调用绑定来分支您的承诺。
一个分支将决定是否发送消息。由于承诺是链接的,唯一的方法是像在其他分支中一样在整个承诺链中传递状态。
let exists = db.collection('employee')
.find({
email: data.email
})
.toArray()
exists.then((checkEmail)=>{
if(checkEmail.length > 0){
return res.send({ status: 0, message: 'Employee already exist.'});
//ends the excution here
}
})
exists.then((checkEmail)=>{
return checkEmail.length === 0;
}
})
.then((createUser) => {
if(createUser){
return db.collection('employee').insert({
//...
})
else
return createUser
}
})
.then((createUser)=> {
if(createUser) {
// Other stuff
}
})
.catch((err)=>{
console.log(error)
res.send({ status: 0, message: 'Something went wrong.' });
})
我正在使用 mongodb 制作后端 API。而且我用过bluebird
来使用promise.
return new promise((resolve, reject) => {
db.collection('employee').find({
email: data.email
}).toArray().then((checkEmail) => {
if (checkEmail.length > 0) {
res.send({ status: 0, message: 'Employee already exist.' });
// I want to stop my node hear.
// I have tried return false , but not works.
}
}).then(() => {
// Add employee details into collection as a new employee.
return db.collection('employee').insert({
//...
})
}).then((employee) => {
// Other stuff
}).catch((error) => {
console.log(error)
res.send({ status: 0, message: 'Something went wrong.' });
});;
}
如您所见,如果 checkEmail > 0
,那么我已经发送了我在邮递员中得到的正确回复。但我的节点仍在执行下一个代码。
那么当我发回 res 时,我怎么能停止下一次执行呢?
我已经将 res 发送给客户端,然后它也执行下一个代码,在其他部分我也发送了 success/error res。这就是我收到此错误的原因。
Error: Can't set headers after they are sent.
我试过用return
、return false
。但它仍然执行我的下一个代码。
不需要在您的 return 语句中创建新的承诺,如果您需要您的方法来产生承诺,您可以 return 链本身。
从 promise 链中的 then
返回不会停止链,它只是将 returned 值作为参数传递给下一个 then
。绕过它的一种方法是抛出您自己的自定义错误并在 catch
中适当地处理它。这样的事情应该有效:
return db
.collection("employee")
.find({email: data.email})
.toArray()
.then(checkEmail => {
if (checkEmail.length > 0) {
let err = new Error("Employee already exists.");
err.code = "EMPLOYEE_ALREADY_EXISTS";
throw err;
}
})
.then(() => {
// Add employee details into collection as a new employee.
return db.collection("employee").insert({
//...
});
})
.then(employee => {
// Other stuff
})
.catch(error => {
if (error.code && error.code === "EMPLOYEE_ALREADY_EXISTS") {
res.send({ status: 0, message: "Employee already exists." });
} else {
console.log(error);
res.send({ status: 0, message: "Something went wrong." });
}
});
编辑:为了再次说明,第三个 then
中的员工将是您之前 then
中的 return,即 db.collection("employee").insert({...})
returns.
你可以像
一样分支你的承诺db.collection('employee')
.find({
email: data.email
})
.toArray()
.then((checkEmail) => {
if (checkEmail.length > 0) {
return res.send({ status: 0, message: 'Employee already exist.'});
}
else
{
return db.collection('employee').insert({
//...
})
.then((employee) => {
// Other stuff})
})
}
})
.catch((error) => {
console.log(error)
res.send({ status: 0, message: 'Something went wrong.' });
});
或者您可以通过将它与不同的 onSuccess 调用绑定来分支您的承诺。 一个分支将决定是否发送消息。由于承诺是链接的,唯一的方法是像在其他分支中一样在整个承诺链中传递状态。
let exists = db.collection('employee')
.find({
email: data.email
})
.toArray()
exists.then((checkEmail)=>{
if(checkEmail.length > 0){
return res.send({ status: 0, message: 'Employee already exist.'});
//ends the excution here
}
})
exists.then((checkEmail)=>{
return checkEmail.length === 0;
}
})
.then((createUser) => {
if(createUser){
return db.collection('employee').insert({
//...
})
else
return createUser
}
})
.then((createUser)=> {
if(createUser) {
// Other stuff
}
})
.catch((err)=>{
console.log(error)
res.send({ status: 0, message: 'Something went wrong.' });
})