我从同时进入此添加历史记录页面的两个客户端推送子行记录(添加历史记录)时出错。如果只推送子记录逐个客户端(即,输入添加历史记录页面以在客户端1中推送记录,然后输入添加历史记录页面以推送记录),它可以正常工作。
AngularJS中是否需要一些记录锁定处理?
NodeJS服务中的错误是:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.header (/home/oracle/node/ang_backend1/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/home/oracle/node/ang_backend1/node_modules/express/lib/response.js:163:12)
at ServerResponse.json (/home/oracle/node/ang_backend1/node_modules/express/lib/response.js:249:15)
at /home/oracle/node/ang_backend1/routes/datas.js:53:21
at /home/oracle/node/ang_backend1/node_modules/mongoose/lib/model.js:3324:16
at /home/oracle/node/ang_backend1/node_modules/mongoose/lib/document.js:1924:15
at nextTickCallbackWith0Args (node.js:420:9)
at process._tickCallback (node.js:349:13)
它还会导致后端nodejs停止并需要重启。
HistoryaddController
是:
.controller('HistoryaddController', function($scope, $cordovaNetwork, $ionicPopup, $rootScope, $filter, $state, $stateParams, Movie, $ionicLoading, $ionicPlatform, $ionicHistory, $http, $timeout) {
$scope.loadMovie = function() {
$scope.movie = Movie.get({ id: $stateParams.id }).$promise.then(function(results) {
console.log(results);
$scope.movie = results;
});
};
$scope.loadMovie();
$scope.themovie = {
details: '',
};
$scope.updateMovie = function() {
$scope.movie.Histories.push($scope.themovie);
$scope.movie.$update(function() {
$state.go('movies');
});
};
})
此系列的后端mongo模型是:
var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');
mongoose.Promise = global.Promise;
var connection = mongoose.createConnection("mongodb://localhost:27017/mymongodb");
autoIncrement.initialize(connection);
var Schema = mongoose.Schema;
var DataSchema = new Schema({
sort: { type: Schema.Types.ObjectId, ref: 'Sort' },
custname: String,
problem: String,
opendate: { type: Date, default: Date.now },
enddate: { type: Date },
lastupdate: { type: Date },
assignedon: { type: Date },
openby: String,
updateby: String,
assignedto: String,
severity: String,
product: String,
srstatus: String,
reportby: String,
custowner: String,
vendorid: String,
Histories: [{
details: String
}]
});
DataSchema.plugin(autoIncrement.plugin, 'Data');
module.exports = mongoose.model('Data', DataSchema);
后端路线是:
var Data = require('../app/models/data'); // get the mongoose model
var express = require('express');
var router = express.Router();
router.route('/:id')
.put(function(req, res) {
Data.findOne({ _id: req.params.id }, function(err, data) {
if (err)
res.send(err);
for (prop in req.body) {
data[prop] = req.body[prop];
}
// save the movie
data.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Data updated!' });
});
});
})
<form name="myForm1">
<ion-view title="Add Detail">
<ion-header-bar align-title="center" class="bar-balanced">
<ion-nav-bar align-title="center" class="nav-title-slide-ios7 bar-balanced">
<ion-nav-back-button class="button-icon ion-arrow-left-c">
</ion-nav-back-button>
</ion-nav-bar>
</ion-header-bar>
<ion-content overflow-scroll="true" class="padding">
<div class="list" >
<label class="item item-input">
<input type="text" placeholder="customer" ng-model="movie.custname" required>
</label>
<label class="item item-input">
<input type="text" placeholder="problem" ng-model="movie.problem" required>
</label>
<label class="item item-input">
<textarea ng-trim="false" rows=8 placeholder="Details" ng-model="themovie.details" required></textarea>
</label>
</div>
<button class="button button-full button-balanced" clicked-disable ng-disabled="myForm1.$invalid" ng-click="updateMovie()">
Add
</button>
</ion-content>
</ion-view>
</form>
赞赏你是否可以指出我应该解决的问题。我得到的问题是当两个客户端应用程序(即客户端app1和客户端应用程序2)进入此Historiesadd页面时 - &gt;输入记录 - &gt;保存。它会得到这个错误。如果client1进入此Historiesadd页面 - &gt;输入记录 - &gt;保存。然后client2进入此Historiesadd页面 - &gt;输入记录 - &gt;保存。它不会得到这个错误。这意味着,第二个客户端无法进入Histroiesadd页面,直到第一个客户端成功保存记录。它可以避免这个错误。我不确定它是后端节点表达问题还是fontend Angular。感谢
答案 0 :(得分:0)
我认为问题依赖于此。
for (prop in req.body) {
data[prop] = req.body[prop];
}
// save the movie
data.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Data updated!' });
});
您尝试在重新发送后将json重新发送回客户端。确保在返回json之前已完成保存。尝试将res.json置于回调之外
// save the movie
data.save(function(err) {
if (err)
res.send(err);
});
res.json({ message: 'Data updated!' });