未捕获的异常" MongoCursorException" for PHP和MongoDB

时间:2018-03-28 07:41:59

标签: php mongodb

错误消息

我的PHP代码存在问题,我在网页上收到此错误消息(见下文)。它是什么意思,我该如何解决?

Fatal error: Uncaught exception 'MongoCursorException' with message 'localhost:27017: Can't canonicalize query: TypeMismatch: "$search" had the wrong type. Expected String, found NULL'

PHP

这是我的PHP代码:

<?php
//Connect to MongoDB
$mongoClient = new MongoClient();

//Select a database
$db = $mongoClient->ecommerce;

//Extract the data that was sent to the server
$search_string = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING);

//Create a PHP array with our search criteria
$findCriteria = [
    '$text' => ['$search' => $search_string] 
];

//Find all of the customers that match  this criteria
$cursor = $db->products->find($findCriteria);

//Output the results
echo "<h1>Results</h1>";
foreach ($cursor as $cust) {
    echo "<p>";
    echo "product name: " . $cust['name'];
    echo "</p>";
}

//Close the connection
$mongoClient->close();
?>

1 个答案:

答案 0 :(得分:1)

这是因为$ search_string为null而不是字符串,如错误消息所示。

在执行操作之前检查private void updatebtn_Click(object sender, EventArgs e) { vehimodel.Vehicle_No = vehiclenotextbox.Text.Trim(); vehimodel.Name = nametextbox.Text.Trim(); vehimodel.Mobile = mobiletextbox.Text.Trim(); vehimodel.Email_ID = emailtextbox.Text.Trim(); vehimodel.Make_Model = makeandmodeltextbox.Text.Trim(); using(ShivaniVehicleDbContext db = new ShivaniVehicleDbContext()) { db.Entry(vehimodel).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); MessageBox.Show("Sucessfully Created"); } } 是否为空。要解决此问题,请从表单中对此文件发出POST请求,或根据需要更改类型。