上で動作していない - アカウント登録のために - $アヤックス成功パラメータに、多くの検索の後、私はできません以下のコードで何が間違っているかを確認してください。NodeJSのasynchronous.end()私は<strong>データベースクエリ</strong>に基づいてテキストを返すようにしようとしていますPOSTリクエスト
非同期機能を必要とするhttp応答を送信する方法がわかりません。しようとするとリクエストが処理されないか、まったく検出されません。
私の問題は、私のres.end( "false")呼び出しが時間通りに呼び出されないことだと思いますが、コードは私に似ています。私は、問題は、私は
クライアント側のコメント入れserver.jsであると確信しているが、私が急行し、すべてのコールバックを使用したくない
が正常に動作している:
$.ajax({
async: true,
dataType: "text",
type: 'POST',
url: 'http://192.168.0.23:3000',
data: JSON.stringify(account_info),
contentType: 'application/json; charset=utf-8',
success: function (res) {
console.log('Account registration : ' + res);
},
complete: function (res) {
console.log('Account registration complete : ' +
JSON.stringify(res));
},
error: function (err) {
console.log(err.responseText)
}
});
サーバー側:
server.js
const http = require('http');
var mongoose = require('mongoose');
var Visitor = require('./models/visitor.js');
var Account = require('./models/account.js');
var api = require('./controllers/api.js');
var isExisting = api.isExisting;
var saveData = api.saveData;
const port = 3000;
const server = http.createServer();
console.log('server is listening on ' + port);
server.listen(port);
server.on('request', function (request, response) {
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Methods', 'POST');
response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
console.log(request.method);
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function() {
//In case there's content in the POST request
if (body) {
console.log('\nRequest content:' + body + '\n');
body = JSON.parse(body);
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/someDB', {
useMongoClient: true
});
//Pattern = ACCOUNT_REGISTRATION
if (body.pattern == 'account_registration') {
var value = {
email: body.email
}
var new_data = new Account(Account.store(body));
//Check if account_name or website URL already in db
// exist returning the callback
isExisting(Account, value, function (exist) {
console.log(exist);
if (!exist) {
saveData(new_data);
//If you dont remove this line, the request is not detected by nodeJS
response.end('true');
} else {
console.log('\nAccount already exist.');
//If you dont remove this line, the request is not detected by nodeJS
response.end('false');
mongoose.connection.close();
}
});
}
}
//Here it's working good but If I remove this line it'll not handle the request at all
response.end('oko');
});
});
api.js
// The API controller
var mongoose = require('mongoose');
//Send some new_data to db
exports.saveData = function (new_data) {
//Data saving into MongoDB database
new_data.save(function (err) {
if (err) {
throw err;
}
console.log('\nData successfully added.');
// We need to disconnect now
mongoose.connection.close();
});
}
exports.isExisting = function (ModelName, value, callback) {
ModelName.count(value, function (err, count) {
if (err)
throw err;
if (count == 0)
callback(false);
else
callback(true);
});
}
LAST EDIT:要するに、
この
は私が最後の行を削除しない(通常の動作を得るものですしかし 私の非同期応答が得られませんserver is listening on 3000
OPTIONS
POST Request content:{"*****"}//real data already in db
true //This is isExisting() callback
Account already exist.
しかし、私は最後のResponse.Endの( 'OKO')を削除すると、OPTIONS後のすべてが表示されない...
'場合(!存在する){SaveDataを(NEW_DATA)と答えた場合でも、すべての要求が答えを受けることを確認して安全であると
。 } '既存のアカウントがない場合は、どこに返信しますか? saveData()にはありません。あなたが削除したい 'response.end( 'oko')'行によって送信されると仮定します。存在する){saveData(new_data); response.end( 'oko'); } ' – Shilly
あなたの編集がそれを説明すると思います。 – Shilly
response.end( 'oko')を削除した場合。要求はもう処理されませんが、上記のコードでは、最後のものを削除しても、isExist()の近くで行ったすべてのresponse.end呼び出しは実行されません... – nQuery