我所拥有的是1000个不同领域的数据。我意识到,如果从excel导入的数据是字符串,我无法获得我的mongodb中的平均值。什么是让我的所有1000个数据都成为数字的最简单方法。
到目前为止:
Server.js
var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('meibanlist', ['contact']);
var bodyParser = require('body-parser');
var async = require('async');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.get('/meibanlist', function (req, res) {
console.log('I received a GET request');
db.meibanlist.find(function (err, docs) {
console.log(docs);
res.json(docs);
});
});
app.post('/meibanlist', function (req, res) {
console.log(req.body);
db.meibanlist.insert(req.body, function(err, doc) {
res.json(doc);
});
});
app.delete('/meibanlist/', function (req, res) {
console.log();
db.meibanlist.remove(function (err, doc) {
res.json(doc);
});
});
app.get('/meibanlist/', function (req, res) {
console.log();
db.meibanlist.findOne(function (err, doc) {
res.json(doc);
});
});
app.put('/meibanlist/', function (req, res) {
console.log(req.body.machine_Id);
db.meibanlist.findAndModify({
query: {_id: mongojs.machine_Id},
update: {$set: {machine_Id: req.body.machine_Id, air_Temp: req.body.air_Temp, water_Temp: req.body.water_Temp, heat_Temp: req.body.heat_Temp, room_Temp: req.body.room_Temp, date:req.body.date, time: req.body.time}},
new: true}, function (err, doc) {
res.json(doc);
}
);
});
app.get('/convertStringsToNumbe' , function(req, res){
var recUpdated = 0;
db.contact.find({}, function(err , meibanlist){
if(meibanlist.length){
async.eachSeries(meibanlist , function(listItem , cb){
if(listItem && typeof listItem.air_Temp == 'string'){
db.contact.update(
{ machine_Id: listItem.machine_Id},
{ $set : { "air_Temp" : parseInt(listItem.air_Temp) } }
,function(err , updatedRec){
recUpdated++;
cb();
});
}
else{
console.log('already String')
cb();
}
} , function(){
console.log('operation completed, records updated count = ' + recUpdated);
return true;
})
}
else{
console.log('no data returned');
}
})
});
app.listen(3000);
console.log('3000');
的index.html
<!DOCTYPE>
<html ng-app="myApp">
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<title>Meiban App</title>
</head>
<body>
<div class="container" ng-controller="AppCtrl">
<h1>Meiban App</h1>
<table class="table">
<thead>
<tr>
<th>machine_Id</th>
<th>air_temp</th>
<th>water_temp</th>
<th>heat_temp</th>
<th>room_temp</th>
<th>date</th>
<th>time</th>
<th>Action</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="form-control" ng-model="contact.machine_Id" ></td>
<td><input class="form-control" ng-model="contact.air_temp"></td>
<td><input class="form-control" ng-model="contact.water_temp"></td>
<td><input class="form-control" ng-model="contact.heat_temp"></td>
<td><input class="form-control" ng-model="contact.room_temp"></td>
<td><input class="form-control" ng-model="contact.time"></td>
<td><input class="form-control" ng-model="contact.date"></td>
<td><button class="btn btn-primary" ng-click="addContact()">Add Collection</button></td>
<td><button class="btn btn-info" ng-click="update()">Update</button> <button class="btn btn-info" ng-click="deselect()">Clear</button></td>
</tr>
<tr ng-repeat="contact in meibanlist">
<td>{{contact.machine_Id }}</td>
<td>{{contact.air_temp}}</td>
<td>{{contact.water_temp}}</td>
<td>{{contact.heat_temp}}</td>
<td>{{contact.room_temp}}</td>
<td>{{contact.date}}</td>
<td>{{contact.time}}</td>
<td><button class="btn btn-danger" ng-click="remove(contact._id)">Remove</button></td>
<td><button class="btn btn-warning" ng-click="edit(contact._id)">Edit</button></td>
</tr>
</tbody>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
<script src="controller/controller.js"></script>
</body>
</html>
controller.js
var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");
var refresh = function() {
$http.get('/contactlist').success(function(response) {
console.log("I got the data I requested");
$scope.contactlist = response;
$scope.contact = "";
});
};
refresh();
$scope.addContact = function() {
console.log($scope.contact);
$http.post('/contactlist', $scope.contact).success(function(response) {
console.log(response);
refresh();
});
};
$scope.remove = function(id) {
console.log(id);
$http.delete('/contactlist/' + id).success(function(response) {
refresh();
});
};
$scope.edit = function(id) {
console.log(id);
$http.get('/contactlist/' + id).success(function(response) {
$scope.contact = response;
});
};
$scope.update = function() {
console.log($scope.contact._id);
$http.put('/contactlist/' + $scope.contact._id, $scope.contact).success(function(response) {
refresh();
});
};
$scope.deselect = function() {
$scope.contact = "";
};
}]);
它给了我这个错误:
TypeError: db.contact.find(...).then is not a function
at /home/balkis96/FYP/Meiban/server.js:55:27
at Layer.handle [as handle_request] (/home/balkis96/node_modules/express/lib/router/layer.js:95:5)
at next (/home/balkis96/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/balkis96/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/balkis96/node_modules/express/lib/router/layer.js:95:5)
at /home/balkis96/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/balkis96/node_modules/express/lib/router/index.js:330:12)
at next (/home/balkis96/node_modules/express/lib/router/index.js:271:10)
at jsonParser (/home/balkis96/node_modules/body-parser/lib/types/json.js:103:7)
at Layer.handle [as handle_request] (/home/balkis96/node_modules/express/lib/router/layer.js:95:5)
答案 0 :(得分:0)
执行npm install async --save
var async = require('async');
app.get('/convertStringsToNumbe' , function(req, res){
var recUpdated = 0;
db.contact.find({}).then(function(meibanlist){
if(meibanlist.length){
async.eachSeries(meibanlist , function(listItem , cb){
if(listItem && typeof listItem.air_Temp == 'string'){
db.contact.update(
{ machine_Id: listItem.machine_Id},
{ $set : { "air_Temp" : parseInt(listItem.air_Temp) } }
).then(function(){
recUpdated++;
cb();
});
}
else{
console.log('already String')
cb();
}
} , function(){
console.log('operation completed, records updated count = ' + recUpdated);
return true;
})
}
else{
console.log('no data returned);
}
})
});
使用邮递员或只是在浏览器中点击此路线