0

Google 데이터 저장소에서 엔티티 데이터를 설정하고 가져 오는 방법을 파악하는 데 문제가 있습니다. 나는 여러 가지 예를 발견했으며 어느 것이 옳은 것인지 이해하지 못합니다. 아래는 지금까지 내가 한 것입니다. 이 오류가 계속 발생합니다 : ReferenceError: datastore is not defined. 데이터 저장소 API를 올바르게 호출하지 않습니까?노드를 사용하여 Google Datastore에서 엔티티 데이터를 설정하고 수신하는 방법

server.js 파일 :

var express = require('express'); 
var app = express(); 

const Datastore = require('@google-cloud/datastore'); 
const datastore = Datastore(); 

require('./routes/main')(app); 

require('@google/cloud-debug').start({ 
    keyFilename: './jarvis-hd-live-842e4f78479e.json' 
}); 

const PORT = process.env.PORT || 8080; 

app.listen(PORT,() => { 
    console.log(`Your app is listening on port ${PORT}`); 
}); 

루트 파일 :

데이터 스토어의 SDK가 인증되지
app.post('/message', function (request, response) { 
    message = request.body.Body; 

    response.send("<Response><Message>Heyyo!</Message></Response>"); 

    const key = datastore.key('timestamp'); 

    datastore.save({ 
    key: key, 
    data : { 
     timestamp_value: 0 
    } 
    }); 


    datastore.insert(entity) 
     .then(()=> { 
      console.log("Data object inserted successfully."); 
     }); 

}); 

답변

1

/적절한 자격 증명을 사용하여 초기화.

server.js는해야한다 -

같은 라인에
var express = require('express'); 
var app = express(); 
//The key file is required in a variable. The variable is now a JSON object with the credentials. 
var credentials = require('./jarvis-hd-live-842e4f78479e.json'); 
const Datastore = require('@google-cloud/datastore'); 
//Initialise the datastore object with the proper project id and credentials. 
const datastore = Datastore({ 
    projectId: "your_project_id_goes_here", 
    credentials: credentials 
}); 
/* 
* If you wish to give the filename instead of the credentials object while initialising, replace the credential key with keyFileName and the value with the file name of the key file. 
* Note: The key file should be in the same directory. If another directory, please provide absolute path. 
*/ 
require('./routes/main')(app); 
require('@google/cloud-debug').start({ 
    credentials: credentials 
}); 
const PORT = process.env.PORT || 8080; 

app.listen(PORT,() => { 
    console.log(`Your app is listening on port ${PORT}`); 
}); 

, routes.js은 다음과 같이됩니다 -

app.post('/message', function (request, response) { 
    message = request.body.Body; 

response.send("<Response><Message>Heyyo!</Message></Response>"); 

//While getting a key for an object, you need to mention the namespace on which your kind resides.(Kind can be thought of as a table.) 
const key = datastore.key({ 
    namespace: "your_namespace_goes_here", 
    path: ["kind_name_goes_here","explicit_name/id_of_the_object"] 
}); 
/* 
* If the name/id is kept null in the path key, datastore will assign a numeric id to the key. 
* The name should be set if the application uses some kind of custom naming/identification scheme using UUID/GUID. 
*/ 
datastore.save({ 
    key: key, 
    data : { 
     timestamp_value: 0 
    } 
}); 
datastore.insert(entity) 
    .then(()=> { 
     console.log("Data object inserted successfully."); 
    }); 
}); 
+0

감사합니다! 이것에 대한 지시 사항을 어디서 찾았습니까? –

+0

항상 https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/0.5.1/datastore에서 관련 문서를 참조하십시오. 문서가 현재 약간 울퉁불퉁하다는 것을 알고 있습니다. –

+0

감사합니다! 문서에는 여러 가지 예가 많이있었습니다. 이것은 많은 도움이되었습니다. –

관련 문제